Skip to content

Commit 168092e

Browse files
authored
[DirectX] Legalize Freeze instruction (#136043)
fixes #135719 LLVM 3.7 did not have a freeze instruction Further this instruction is really only used as syntactic sugar in LLVM's optimizer passes to not aggressively optimize things that could be undef or poison ie x*2 to x+x. Most backends treat it as a no-op so we will do the same by removing it and replacing its uses with its input.
1 parent 8311620 commit 168092e

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

llvm/lib/Target/DirectX/DXILLegalizePass.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020

2121
using namespace llvm;
2222

23+
static void legalizeFreeze(Instruction &I,
24+
SmallVectorImpl<Instruction *> &ToRemove,
25+
DenseMap<Value *, Value *>) {
26+
auto *FI = dyn_cast<FreezeInst>(&I);
27+
if (!FI)
28+
return;
29+
30+
FI->replaceAllUsesWith(FI->getOperand(0));
31+
ToRemove.push_back(FI);
32+
}
33+
2334
static void fixI8TruncUseChain(Instruction &I,
2435
SmallVectorImpl<Instruction *> &ToRemove,
2536
DenseMap<Value *, Value *> &ReplacedValues) {
@@ -169,6 +180,7 @@ class DXILLegalizationPipeline {
169180
void initializeLegalizationPipeline() {
170181
LegalizationPipeline.push_back(fixI8TruncUseChain);
171182
LegalizationPipeline.push_back(downcastI64toI32InsertExtractElements);
183+
LegalizationPipeline.push_back(legalizeFreeze);
172184
}
173185
};
174186

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
3+
; RUN: opt -S -passes='dxil-legalize' -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
4+
5+
6+
define i32 @test_remove_freeze(i32 %x) {
7+
; CHECK-LABEL: define i32 @test_remove_freeze(
8+
; CHECK-SAME: i32 [[X:%.*]]) {
9+
; CHECK-NEXT: [[ENTRY:.*:]]
10+
; CHECK-NEXT: [[Y:%.*]] = add i32 [[X]], 1
11+
; CHECK-NEXT: ret i32 [[Y]]
12+
;
13+
entry:
14+
%f = freeze i32 %x
15+
%y = add i32 %f, 1
16+
ret i32 %y
17+
}

0 commit comments

Comments
 (0)