diff --git a/IGC/AdaptorCommon/FastMathConstantHandling.cpp b/IGC/AdaptorCommon/FastMathConstantHandling.cpp new file mode 100644 index 000000000000..485b8844dec5 --- /dev/null +++ b/IGC/AdaptorCommon/FastMathConstantHandling.cpp @@ -0,0 +1,103 @@ +/*========================== begin_copyright_notice ============================ + +Copyright (C) 2023 Intel Corporation + +SPDX-License-Identifier: MIT + +============================= end_copyright_notice ===========================*/ + +#include "common/LLVMWarningsPush.hpp" +#include +#include +#include +#include "common/LLVMWarningsPop.hpp" +#include "FastMathConstantHandling.h" +#include "Compiler/IGCPassSupport.h" +#include "common/igc_regkeys.hpp" + +using namespace llvm; +namespace IGC +{ + +class FastMathConstantHandling : public FunctionPass, public InstVisitor +{ +public: + FastMathConstantHandling(); + ~FastMathConstantHandling() {} + static char ID; + bool runOnFunction(Function& F) override; + void visitInstruction(Instruction& I); + virtual llvm::StringRef getPassName() const override { return "Fast Math Constant Handling"; } + + void getAnalysisUsage(AnalysisUsage& AU) const override + { + AU.setPreservesCFG(); + } +}; + +#define PASS_FLAG "FastMathConstantHandling" +#define PASS_DESC "Fast Math Constant Handling" +#define PASS_CFG_ONLY false +#define PASS_ANALYSIS false +IGC_INITIALIZE_PASS_BEGIN(FastMathConstantHandling, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS) +IGC_INITIALIZE_PASS_END(FastMathConstantHandling, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS) + +char FastMathConstantHandling::ID = 0; + +FastMathConstantHandling::FastMathConstantHandling() + :FunctionPass(ID) +{ + initializeFastMathConstantHandlingPass(*PassRegistry::getPassRegistry()); +} + +// This purpose of this pass is to catch bad fast flag management where we are seeing +// constants that are not matching the fast flags that are set on the instructions + +void FastMathConstantHandling::visitInstruction(Instruction& I) +{ + if (isa(I)) + { + struct BoolSpecialConstants { + bool hasInf = false; + bool hasNan = false; + bool hasNegZero = false; + } BSC; + + for (auto &Op : I.operands()) + { + if (auto* fp_val = dyn_cast(Op)) + { + auto APF = fp_val->getValueAPF(); + BSC.hasInf |= APF.isInfinity(); + BSC.hasNan |= APF.isNaN(); + BSC.hasNegZero |= APF.isNegZero(); + } + } + + if (BSC.hasInf) + I.setHasNoInfs(false); + + if (BSC.hasNan) + I.setHasNoNaNs(false); + + if (BSC.hasNegZero) + I.setHasNoSignedZeros(false); + } +} + +bool FastMathConstantHandling::runOnFunction(Function& F) +{ + if (IGC_IS_FLAG_DISABLED(DisableFastMathConstantHandling)) + { + visit(F); + } + return true; +} + + +FunctionPass* createFastMathConstantHandling() +{ + return new FastMathConstantHandling(); +} + +}//namespace IGC diff --git a/IGC/AdaptorCommon/FastMathConstantHandling.h b/IGC/AdaptorCommon/FastMathConstantHandling.h new file mode 100644 index 000000000000..5f37abba6817 --- /dev/null +++ b/IGC/AdaptorCommon/FastMathConstantHandling.h @@ -0,0 +1,19 @@ +/*========================== begin_copyright_notice ============================ + +Copyright (C) 2023 Intel Corporation + +SPDX-License-Identifier: MIT + +============================= end_copyright_notice ===========================*/ + +#pragma once + +#include "common/LLVMWarningsPush.hpp" +#include "llvm/Pass.h" +#include "common/LLVMWarningsPop.hpp" + +namespace IGC +{ + void initializeFastMathConstantHandlingPass(llvm::PassRegistry&); + llvm::FunctionPass* createFastMathConstantHandling(); +} diff --git a/IGC/DriverInterface/CMakeLists.txt b/IGC/DriverInterface/CMakeLists.txt index 0fdaa67762e7..021ec5df7aea 100644 --- a/IGC/DriverInterface/CMakeLists.txt +++ b/IGC/DriverInterface/CMakeLists.txt @@ -20,6 +20,7 @@ set(IGC_BUILD__SRC__DriverInterface "${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/TypesLegalizationPass.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/LegalizeFunctionSignatures.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/DivergentBarrierPass.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/FastMathConstantHandling.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorOCL/OCL/LoadBuffer.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorOCL/OCL/Patch/patch_parser.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorOCL/OCL/Platform/cmd_media_caps_g8.cpp" @@ -83,6 +84,7 @@ endif(IGC_BUILD__SPIRV_ENABLED) set(IGC_BUILD__HDR__DriverInterface "${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/customApi.hpp" "${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/DivergentBarrierPass.h" + "${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/FastMathConstantHandling.h" "${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorOCL/OCL/KernelAnnotations.hpp" "${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorOCL/OCL/CommandStream/SamplerTypes.h" "${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorOCL/OCL/CommandStream/SurfaceTypes.h" diff --git a/IGC/common/igc_flags.h b/IGC/common/igc_flags.h index 68b29fcce181..b800f06698b3 100644 --- a/IGC/common/igc_flags.h +++ b/IGC/common/igc_flags.h @@ -757,6 +757,7 @@ DECLARE_IGC_REGKEY(bool, OverrideCsTileLayout, 0, "Override compute wa DECLARE_IGC_REGKEY(DWORD, MemCpyLoweringUnrollThreshold, 12, "Min number of mem instructions that require non-unrolled loop when lowering memcpy", false) DECLARE_IGC_REGKEY(bool, EnableSOAPromotionDisablingHeuristic, false, "Enable heuristic to disable SOA promotion when it may be not beneficial", false) DECLARE_IGC_REGKEY(bool, EnableCSContentCheck, false, "Enable CS content check to force SIMD32", true) +DECLARE_IGC_REGKEY(bool, DisableFastMathConstantHandling, false, "Disable Fast Math Constant Handling", true) DECLARE_IGC_GROUP("Generating precompiled headers") DECLARE_IGC_REGKEY(bool, ApplyConservativeRastWAHeader, true, "Apply WaConservativeRasterization for the platforms enabled", false)