Skip to content

Commit 393c54a

Browse files
PawelJurekigcbot
authored andcommitted
RemoveCodeAssumptions pass
Instruction combining can miss some optimizations if it happens, that the only use of instructions end up in llvm.assume intrinsic. This change removes llvm.assume calls and runs instruction combining before Legalization pass.
1 parent e2e92c0 commit 393c54a

File tree

6 files changed

+137
-2
lines changed

6 files changed

+137
-2
lines changed

IGC/Compiler/CISACodeGen/ShaderCodeGen.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ SPDX-License-Identifier: MIT
126126
#include "Compiler/InitializePasses.h"
127127
#include "Compiler/GenRotate.hpp"
128128
#include "Compiler/Optimizer/Scalarizer.h"
129+
#include "Compiler/RemoveCodeAssumptions.hpp"
129130
#include "common/debug/Debug.hpp"
130131
#include "common/igc_regkeys.hpp"
131132
#include "common/debug/Dump.hpp"
@@ -905,9 +906,9 @@ static void AddLegalizationPasses(CodeGenContext& ctx, IGCPassManager& mpm, PSSi
905906
}
906907
}
907908

908-
// Enabling half promotion AIL for compute shaders only at this point.
909+
// Enabling half promotion AIL for compute shaders only at this point.
909910
// If needed ctx.type check can be removed to apply for all shader types
910-
if (IGC_IS_FLAG_ENABLED(ForceHalfPromotion) ||
911+
if (IGC_IS_FLAG_ENABLED(ForceHalfPromotion) ||
911912
(ctx.getModuleMetaData()->compOpt.WaForceHalfPromotion && ctx.type == ShaderType::COMPUTE_SHADER) ||
912913
(!ctx.platform.supportFP16() && IGC_IS_FLAG_ENABLED(EnableHalfPromotion)))
913914
{
@@ -940,12 +941,20 @@ static void AddLegalizationPasses(CodeGenContext& ctx, IGCPassManager& mpm, PSSi
940941
mpm.add(createNanHandlingPass());
941942
}
942943

944+
if(!isOptDisabled) {
945+
// Removing code assumptions can enable some InstructionCombining optimizations.
946+
// Last instruction combining pass needs to be before Legalization pass, as it can produce illegal instructions.
947+
mpm.add(new RemoveCodeAssumptions());
948+
mpm.add(createIGCInstructionCombiningPass());
949+
}
950+
943951
// TODO: move to use instruction flags
944952
// to figure out if we need to preserve Nan
945953
bool preserveNan = !ctx.getCompilerOption().NoNaNs;
946954

947955
// Legalizer does not handle constant expressions
948956
mpm.add(new BreakConstantExpr());
957+
949958
mpm.add(new Legalization(preserveNan));
950959

951960
// Scalarizer in codegen to handle the vector instructions

IGC/Compiler/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ set(IGC_BUILD__SRC__Compiler
6666
"${CMAKE_CURRENT_SOURCE_DIR}/SampleMultiversioning.cpp"
6767
"${CMAKE_CURRENT_SOURCE_DIR}/HandleFRemInstructions.cpp"
6868
"${CMAKE_CURRENT_SOURCE_DIR}/GenRotate.cpp"
69+
"${CMAKE_CURRENT_SOURCE_DIR}/RemoveCodeAssumptions.cpp"
6970
"${IGC_BUILD__GFX_DEV_SRC_DIR}/skuwa/ibdw_wa.c"
7071
"${IGC_BUILD__GFX_DEV_SRC_DIR}/skuwa/ichv_wa.c"
7172
"${IGC_BUILD__GFX_DEV_SRC_DIR}/skuwa/ibxt_wa.c"
@@ -157,6 +158,7 @@ set(IGC_BUILD__HDR__Compiler
157158
"${CMAKE_CURRENT_SOURCE_DIR}/HandleFRemInstructions.hpp"
158159
"${CMAKE_CURRENT_SOURCE_DIR}/GenRotate.hpp"
159160
"${CMAKE_CURRENT_SOURCE_DIR}/SamplerPerfOptPass.hpp"
161+
"${CMAKE_CURRENT_SOURCE_DIR}/RemoveCodeAssumptions.hpp"
160162
${IGC_BUILD__HDR__Compiler_CISACodeGen}
161163
${IGC_BUILD__HDR__Compiler_DebugInfo}
162164
${IGC_BUILD__HDR__Compiler_Legalizer}

IGC/Compiler/InitializePasses.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,5 @@ void initializeCatchAllLineNumberPass(llvm::PassRegistry&);
226226
void initializePromoteConstantStructsPass(llvm::PassRegistry&);
227227
void initializeLowerInvokeSIMDPass(llvm::PassRegistry&);
228228
void initializeImplicitGIDRestoringPass(llvm::PassRegistry&);
229+
void initializeRemoveCodeAssumptionsPass(llvm::PassRegistry&);
230+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*========================== begin_copyright_notice ============================
2+
3+
Copyright (C) 2022 Intel Corporation
4+
5+
SPDX-License-Identifier: MIT
6+
7+
============================= end_copyright_notice ===========================*/
8+
9+
#include "Compiler/RemoveCodeAssumptions.hpp"
10+
#include "Compiler/IGCPassSupport.h"
11+
12+
using namespace llvm;
13+
using namespace IGC;
14+
15+
// Register pass to igc-opt
16+
#define PASS_FLAG "igc-remove-code-assumptions"
17+
#define PASS_DESCRIPTION "Remove code assumptions from the module"
18+
#define PASS_CFG_ONLY false
19+
#define PASS_ANALYSIS false
20+
IGC_INITIALIZE_PASS_BEGIN(RemoveCodeAssumptions, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
21+
IGC_INITIALIZE_PASS_END(RemoveCodeAssumptions, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
22+
23+
char RemoveCodeAssumptions::ID = 0;
24+
25+
RemoveCodeAssumptions::RemoveCodeAssumptions()
26+
: FunctionPass(ID)
27+
{
28+
initializeRemoveCodeAssumptionsPass(*PassRegistry::getPassRegistry());
29+
}
30+
31+
bool RemoveCodeAssumptions::runOnFunction(Function& F)
32+
{
33+
visit(F);
34+
35+
bool changed = m_instructionsToRemove.size() > 0;
36+
for (auto I : m_instructionsToRemove)
37+
{
38+
I->eraseFromParent();
39+
}
40+
m_instructionsToRemove.clear();
41+
42+
return changed;
43+
}
44+
45+
46+
void RemoveCodeAssumptions::visitIntrinsicInst(llvm::IntrinsicInst& I)
47+
{
48+
auto intrinsicID = I.getIntrinsicID();
49+
50+
switch (intrinsicID)
51+
{
52+
case Intrinsic::assume:
53+
m_instructionsToRemove.push_back(&I);
54+
break;
55+
default:
56+
break;
57+
}
58+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*========================== begin_copyright_notice ============================
2+
3+
Copyright (C) 2022 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 <llvm/IR/Function.h>
14+
#include <llvm/IR/InstVisitor.h>
15+
#include "common/LLVMWarningsPop.hpp"
16+
17+
namespace IGC
18+
{
19+
class RemoveCodeAssumptions : public llvm::FunctionPass, public llvm::InstVisitor<RemoveCodeAssumptions>
20+
{
21+
public:
22+
static char ID;
23+
24+
RemoveCodeAssumptions();
25+
~RemoveCodeAssumptions() {}
26+
27+
virtual bool runOnFunction(llvm::Function& F) override;
28+
29+
virtual llvm::StringRef getPassName() const override
30+
{
31+
return "RemoveCodeAssumptions Pass";
32+
}
33+
34+
void visitIntrinsicInst(llvm::IntrinsicInst& I);
35+
36+
private:
37+
std::vector<llvm::Instruction*> m_instructionsToRemove;
38+
};
39+
} // namespace IGC
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
;=========================== begin_copyright_notice ============================
2+
;
3+
; Copyright (C) 2022 Intel Corporation
4+
;
5+
; SPDX-License-Identifier: MIT
6+
;
7+
;============================ end_copyright_notice =============================
8+
9+
; RUN: igc_opt %s -S -o - -igc-remove-code-assumptions | FileCheck %s
10+
11+
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f16:16:16-f32:32:32-f64:64:64-f80:128:128-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-a:64:64-f80:128:128-n8:16:32:64"
12+
13+
define internal spir_func void @test(i64 %in) {
14+
; BASIC TEST CASE
15+
; CHECK-NOT: @llvm.assume
16+
; CHECK: ret void
17+
%cmp = icmp sgt i64 %in, -1
18+
call void @llvm.assume(i1 %cmp) #3
19+
ret void
20+
}
21+
22+
; Function Attrs: nounwind
23+
declare void @llvm.assume(i1) #0
24+
attributes #0 = { nounwind }
25+

0 commit comments

Comments
 (0)