-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/*========================== begin_copyright_notice ============================ | ||
Copyright (C) 2023 Intel Corporation | ||
SPDX-License-Identifier: MIT | ||
============================= end_copyright_notice ===========================*/ | ||
|
||
#include "common/LLVMWarningsPush.hpp" | ||
#include <llvm/IR/Instructions.h> | ||
#include <llvm/IR/InstVisitor.h> | ||
#include <llvm/IR/Operator.h> | ||
#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<FastMathConstantHandling> | ||
{ | ||
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<FPMathOperator>(I)) | ||
{ | ||
struct BoolSpecialConstants { | ||
bool hasInf = false; | ||
bool hasNan = false; | ||
bool hasNegZero = false; | ||
} BSC; | ||
|
||
for (auto &Op : I.operands()) | ||
{ | ||
if (auto* fp_val = dyn_cast<llvm::ConstantFP>(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters