Skip to content

Commit

Permalink
Initialize new FastMathConstantHandling pass
Browse files Browse the repository at this point in the history
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
paigeale authored and igcbot committed Sep 12, 2023
1 parent d386d8e commit a4b3b5b
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
103 changes: 103 additions & 0 deletions IGC/AdaptorCommon/FastMathConstantHandling.cpp
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
19 changes: 19 additions & 0 deletions IGC/AdaptorCommon/FastMathConstantHandling.h
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();
}
2 changes: 2 additions & 0 deletions IGC/DriverInterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions IGC/common/igc_flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a4b3b5b

Please sign in to comment.