|
| 1 | +/*========================== begin_copyright_notice ============================ |
| 2 | +
|
| 3 | +Copyright (C) 2025 Intel Corporation |
| 4 | +
|
| 5 | +SPDX-License-Identifier: MIT |
| 6 | +
|
| 7 | +============================= end_copyright_notice ===========================*/ |
| 8 | + |
| 9 | +#include "Compiler/Optimizer/OpenCLPasses/ProcessBICodeAssumption/ProcessBICodeAssumption.hpp" |
| 10 | +#include "Compiler/IGCPassSupport.h" |
| 11 | + |
| 12 | +#include "common/LLVMWarningsPush.hpp" |
| 13 | +#include <llvm/IR/Function.h> |
| 14 | +#include <llvm/IR/InstVisitor.h> |
| 15 | +#include <llvm/IR/PatternMatch.h> |
| 16 | +#include "common/LLVMWarningsPop.hpp" |
| 17 | + |
| 18 | +#include "Compiler/CodeGenPublic.h" |
| 19 | + |
| 20 | +using namespace llvm; |
| 21 | +using namespace llvm::PatternMatch; |
| 22 | +using namespace IGC; |
| 23 | + |
| 24 | +// OpenCL standard defines built-in variables like get_global_id as size_t, which translates to i64, but in reality |
| 25 | +// many workloads use values that fit in i32. This pass checks if there are assumptions on built-in variables and adds |
| 26 | +// trunc/zext intructions to reflect that the upper 32 bits are not used. This helps instcombine to optimize code after |
| 27 | +// BIImport. |
| 28 | +class ProcessBICodeAssumption : public llvm::FunctionPass, public llvm::InstVisitor<ProcessBICodeAssumption> { |
| 29 | +public: |
| 30 | + static char ID; |
| 31 | + ProcessBICodeAssumption(); |
| 32 | + |
| 33 | + virtual llvm::StringRef getPassName() const override { return "ProcessBICodeAssumption"; } |
| 34 | + |
| 35 | + virtual bool runOnFunction(Function &F) override; |
| 36 | + void visitCallInst(CallInst &I); |
| 37 | + |
| 38 | +private: |
| 39 | + bool matchCmp(ICmpInst::Predicate Pred, ConstantInt *CI); |
| 40 | + bool matchBuiltin(Instruction *I); |
| 41 | + void matchVectorPattern(Instruction *I); |
| 42 | + |
| 43 | + SmallPtrSet<Instruction *, 8> ToTruncate; |
| 44 | +}; |
| 45 | + |
| 46 | +ProcessBICodeAssumption::ProcessBICodeAssumption() : FunctionPass(ID) { |
| 47 | + initializeProcessBICodeAssumptionPass(*PassRegistry::getPassRegistry()); |
| 48 | +} |
| 49 | + |
| 50 | +bool ProcessBICodeAssumption::runOnFunction(Function &F) { |
| 51 | + |
| 52 | + ToTruncate.clear(); |
| 53 | + visit(F); |
| 54 | + |
| 55 | + if (ToTruncate.empty()) |
| 56 | + return false; |
| 57 | + |
| 58 | + // Insert trunc/zext for each matched builtin call. |
| 59 | + for (auto *I : ToTruncate) { |
| 60 | + |
| 61 | + IRBuilder<> Builder(I); |
| 62 | + Builder.SetInsertPoint(I->getNextNode()); |
| 63 | + |
| 64 | + auto Trunc = Builder.CreateTrunc(I, Builder.getInt32Ty()); |
| 65 | + auto Zext = Builder.CreateZExt(Trunc, Builder.getInt64Ty()); |
| 66 | + |
| 67 | + for (auto It = I->use_begin(), E = I->use_end(); It != E; ) { |
| 68 | + auto Use = It++; |
| 69 | + if (Use->getUser() != Trunc) |
| 70 | + Use->set(Zext); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return true; |
| 75 | +} |
| 76 | + |
| 77 | +void ProcessBICodeAssumption::visitCallInst(CallInst &CI) { |
| 78 | + |
| 79 | + Instruction *I = nullptr; |
| 80 | + ICmpInst::Predicate Pred; |
| 81 | + ConstantInt *Const = nullptr; |
| 82 | + |
| 83 | + // Look for assume: |
| 84 | + // %9 = icmp ult i64 %8, 2147483648 |
| 85 | + // call void @llvm.assume(i1 %9) |
| 86 | + if (!match(&CI, m_Intrinsic<Intrinsic::assume>(m_ICmp(Pred, m_Instruction(I), m_ConstantInt(Const))))) |
| 87 | + return; |
| 88 | + |
| 89 | + if (!matchCmp(Pred, Const)) |
| 90 | + return; |
| 91 | + |
| 92 | + if (matchBuiltin(I)) { |
| 93 | + ToTruncate.insert(I); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + matchVectorPattern(I); |
| 98 | +} |
| 99 | + |
| 100 | +// Look for pattern with insertelement/extractelement: |
| 101 | +// %1 = call spir_func i64 @_Z33__spirv_BuiltInGlobalInvocationIdi(i32 0) |
| 102 | +// %2 = insertelement <3 x i64> undef, i64 %1, i32 0 |
| 103 | +// %3 = call spir_func i64 @_Z33__spirv_BuiltInGlobalInvocationIdi(i32 1) |
| 104 | +// %4 = insertelement <3 x i64> %2, i64 %3, i32 1 |
| 105 | +// %5 = call spir_func i64 @_Z33__spirv_BuiltInGlobalInvocationIdi(i32 2) |
| 106 | +// %6 = insertelement <3 x i64> %4, i64 %5, i32 2 |
| 107 | +// %7 = extractelement <3 x i64> %6, i32 0 |
| 108 | +void ProcessBICodeAssumption::matchVectorPattern(Instruction *I) { |
| 109 | + |
| 110 | + Instruction *EE = I; |
| 111 | + |
| 112 | + // Optional select |
| 113 | + // %7 = extractelement <3 x i64> %6, i32 0 |
| 114 | + // %8 = select i1 true, i64 %7, i64 0 |
| 115 | + if (match(EE, m_Select(m_One(), m_Instruction(I), m_Value()))) |
| 116 | + EE = I; |
| 117 | + |
| 118 | + Value *IE = nullptr, *NextIE = nullptr; |
| 119 | + ConstantInt *EConst = nullptr, *IConst = nullptr; |
| 120 | + |
| 121 | + if (!match(EE, m_ExtractElt(m_Value(IE), m_ConstantInt(EConst)))) |
| 122 | + return; |
| 123 | + |
| 124 | + while (match(IE, m_InsertElt(m_Value(NextIE), m_Instruction(I), m_ConstantInt(IConst)))) { |
| 125 | + if (IConst->getZExtValue() == EConst->getZExtValue()) { |
| 126 | + if (matchBuiltin(I)) |
| 127 | + ToTruncate.insert(I); |
| 128 | + return; |
| 129 | + } |
| 130 | + IE = NextIE; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +bool ProcessBICodeAssumption::matchCmp(ICmpInst::Predicate Pred, ConstantInt *CI) { |
| 135 | + switch (Pred) { |
| 136 | + case ICmpInst::ICMP_ULE: |
| 137 | + return CI->getZExtValue() <= llvm::APInt::getMaxValue(32).getZExtValue(); |
| 138 | + case ICmpInst::ICMP_ULT: |
| 139 | + return CI->getZExtValue() <= llvm::APInt::getMaxValue(32).getZExtValue() + 1; |
| 140 | + case ICmpInst::ICMP_SLE: |
| 141 | + return CI->getZExtValue() <= llvm::APInt::getSignedMaxValue(32).getZExtValue(); |
| 142 | + case ICmpInst::ICMP_SLT: |
| 143 | + return CI->getZExtValue() <= llvm::APInt::getSignedMaxValue(32).getZExtValue() + 1; |
| 144 | + default: |
| 145 | + return false; |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +bool ProcessBICodeAssumption::matchBuiltin(Instruction *I) { |
| 150 | + if (auto CI = dyn_cast<CallInst>(I)) { |
| 151 | + return CI->getCalledFunction()->getName() == "_Z33__spirv_BuiltInGlobalInvocationIdi" || |
| 152 | + CI->getCalledFunction()->getName() == "_Z29__spirv_BuiltInGlobalLinearIdv"; |
| 153 | + } |
| 154 | + return false; |
| 155 | +} |
| 156 | + |
| 157 | +// Register pass to igc-opt |
| 158 | +#define PASS_FLAG "igc-process-bi-code-assumption" |
| 159 | +#define PASS_DESCRIPTION "Processes code assumptions assigned to builtin variables" |
| 160 | +#define PASS_CFG_ONLY false |
| 161 | +#define PASS_ANALYSIS false |
| 162 | +IGC_INITIALIZE_PASS_BEGIN(ProcessBICodeAssumption, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS) |
| 163 | +IGC_INITIALIZE_PASS_END(ProcessBICodeAssumption, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS) |
| 164 | + |
| 165 | +char ProcessBICodeAssumption::ID = 0; |
| 166 | + |
| 167 | +FunctionPass *IGC::createProcessBICodeAssumptionPass() { return new ProcessBICodeAssumption(); } |
0 commit comments