Skip to content

Commit 950349f

Browse files
paigealeigcbot
authored andcommitted
Initialize new FastMathConstantHandling pass
This pass solves the issues where the fast math flags on the instruction do not match the immediate constants on that instruction. If we don't match this we can get wrong behavior later in instcombine
1 parent caf028e commit 950349f

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*========================== begin_copyright_notice ============================
2+
3+
Copyright (C) 2023 Intel Corporation
4+
5+
SPDX-License-Identifier: MIT
6+
7+
============================= end_copyright_notice ===========================*/
8+
9+
#include "common/LLVMWarningsPush.hpp"
10+
#include <llvm/IR/Instructions.h>
11+
#include <llvm/IR/InstVisitor.h>
12+
#include <llvm/IR/Operator.h>
13+
#include "common/LLVMWarningsPop.hpp"
14+
#include "FastMathConstantHandling.h"
15+
#include "Compiler/IGCPassSupport.h"
16+
#include "common/igc_regkeys.hpp"
17+
18+
using namespace llvm;
19+
namespace IGC
20+
{
21+
22+
class FastMathConstantHandling : public FunctionPass, public InstVisitor<FastMathConstantHandling>
23+
{
24+
public:
25+
FastMathConstantHandling();
26+
~FastMathConstantHandling() {}
27+
static char ID;
28+
bool runOnFunction(Function& F) override;
29+
void visitInstruction(Instruction& I);
30+
virtual llvm::StringRef getPassName() const override { return "Fast Math Constant Handling"; }
31+
32+
void getAnalysisUsage(AnalysisUsage& AU) const override
33+
{
34+
AU.setPreservesCFG();
35+
}
36+
};
37+
38+
#define PASS_FLAG "FastMathConstantHandling"
39+
#define PASS_DESC "Fast Math Constant Handling"
40+
#define PASS_CFG_ONLY false
41+
#define PASS_ANALYSIS false
42+
IGC_INITIALIZE_PASS_BEGIN(FastMathConstantHandling, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS)
43+
IGC_INITIALIZE_PASS_END(FastMathConstantHandling, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS)
44+
45+
char FastMathConstantHandling::ID = 0;
46+
47+
FastMathConstantHandling::FastMathConstantHandling()
48+
:FunctionPass(ID)
49+
{
50+
initializeFastMathConstantHandlingPass(*PassRegistry::getPassRegistry());
51+
}
52+
53+
// This purpose of this pass is to catch bad fast flag management where we are seeing
54+
// constants that are not matching the fast flags that are set on the instructions
55+
56+
void FastMathConstantHandling::visitInstruction(Instruction& I)
57+
{
58+
if (isa<FPMathOperator>(I))
59+
{
60+
struct BoolSpecialConstants {
61+
bool hasInf = false;
62+
bool hasNan = false;
63+
bool hasNegZero = false;
64+
} BSC;
65+
66+
for (auto &Op : I.operands())
67+
{
68+
if (auto* fp_val = dyn_cast<llvm::ConstantFP>(Op))
69+
{
70+
auto APF = fp_val->getValueAPF();
71+
BSC.hasInf |= APF.isInfinity();
72+
BSC.hasNan |= APF.isNaN();
73+
BSC.hasNegZero |= APF.isNegZero();
74+
}
75+
}
76+
77+
if (BSC.hasInf)
78+
I.setHasNoInfs(false);
79+
80+
if (BSC.hasNan)
81+
I.setHasNoNaNs(false);
82+
83+
if (BSC.hasNegZero)
84+
I.setHasNoSignedZeros(false);
85+
}
86+
}
87+
88+
bool FastMathConstantHandling::runOnFunction(Function& F)
89+
{
90+
if (IGC_IS_FLAG_DISABLED(DisableFastMathConstantHandling))
91+
{
92+
visit(F);
93+
}
94+
return true;
95+
}
96+
97+
98+
FunctionPass* createFastMathConstantHandling()
99+
{
100+
return new FastMathConstantHandling();
101+
}
102+
103+
}//namespace IGC
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*========================== begin_copyright_notice ============================
2+
3+
Copyright (C) 2023 Intel Corporation
4+
5+
SPDX-License-Identifier: MIT
6+
7+
============================= end_copyright_notice ===========================*/
8+
9+
#pragma once
10+
11+
#include "common/LLVMWarningsPush.hpp"
12+
#include "llvm/Pass.h"
13+
#include "common/LLVMWarningsPop.hpp"
14+
15+
namespace IGC
16+
{
17+
void initializeFastMathConstantHandlingPass(llvm::PassRegistry&);
18+
llvm::FunctionPass* createFastMathConstantHandling();
19+
}

IGC/DriverInterface/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ set(IGC_BUILD__SRC__DriverInterface
2020
"${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/TypesLegalizationPass.cpp"
2121
"${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/LegalizeFunctionSignatures.cpp"
2222
"${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/DivergentBarrierPass.cpp"
23+
"${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/FastMathConstantHandling.cpp"
2324
"${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorOCL/OCL/LoadBuffer.cpp"
2425
"${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorOCL/OCL/Patch/patch_parser.cpp"
2526
"${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorOCL/OCL/Platform/cmd_media_caps_g8.cpp"
@@ -83,6 +84,7 @@ endif(IGC_BUILD__SPIRV_ENABLED)
8384
set(IGC_BUILD__HDR__DriverInterface
8485
"${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/customApi.hpp"
8586
"${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/DivergentBarrierPass.h"
87+
"${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/FastMathConstantHandling.h"
8688
"${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorOCL/OCL/KernelAnnotations.hpp"
8789
"${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorOCL/OCL/CommandStream/SamplerTypes.h"
8890
"${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorOCL/OCL/CommandStream/SurfaceTypes.h"

IGC/common/igc_flags.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,7 @@ DECLARE_IGC_REGKEY(bool, OverrideCsTileLayout, 0, "Override compute wa
759759
DECLARE_IGC_REGKEY(DWORD, MemCpyLoweringUnrollThreshold, 12, "Min number of mem instructions that require non-unrolled loop when lowering memcpy", false)
760760
DECLARE_IGC_REGKEY(bool, EnableSOAPromotionDisablingHeuristic, false, "Enable heuristic to disable SOA promotion when it may be not beneficial", false)
761761
DECLARE_IGC_REGKEY(bool, EnableCSContentCheck, false, "Enable CS content check to force SIMD32", true)
762+
DECLARE_IGC_REGKEY(bool, DisableFastMathConstantHandling, false, "Disable Fast Math Constant Handling", true)
762763

763764
DECLARE_IGC_GROUP("Generating precompiled headers")
764765
DECLARE_IGC_REGKEY(bool, ApplyConservativeRastWAHeader, true, "Apply WaConservativeRasterization for the platforms enabled", false)

0 commit comments

Comments
 (0)