Skip to content

Commit 49fcfaa

Browse files
authored
[GVN] Turn off ScalarPRE for TokenLike Types (#156513)
fixes #154407 In HLSL the GVNPass was adding a phi node on a target extention type. https://hlsl.godbolt.org/z/sc14YenEe This is something we cleaned up in a past PR (llvm/llvm-project#154620) by introducing `isTokenLikeTy`. In the case of the GVN pass the target extention type was still making its way through. This change makes it so if we see this type we don't do PRE.
1 parent 3ce0ea3 commit 49fcfaa

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

llvm/lib/Transforms/Scalar/GVN.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2982,7 +2982,8 @@ bool GVNPass::performScalarPREInsertion(Instruction *Instr, BasicBlock *Pred,
29822982
bool GVNPass::performScalarPRE(Instruction *CurInst) {
29832983
if (isa<AllocaInst>(CurInst) || CurInst->isTerminator() ||
29842984
isa<PHINode>(CurInst) || CurInst->getType()->isVoidTy() ||
2985-
CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects())
2985+
CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() ||
2986+
CurInst->getType()->isTokenLikeTy())
29862987
return false;
29872988

29882989
// Don't do PRE on compares. The PHI would prevent CodeGenPrepare from
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -passes=gvn %s | FileCheck %s
3+
4+
; NOTE: A test to confirm GVN doesn't introduce phis for token like types.
5+
; NOTE: This implies the CHECKS should exactly match the IR.
6+
7+
%"$Globals" = type { i32 }
8+
@CBV = external constant %"$Globals"
9+
@Out.str = private unnamed_addr constant [4 x i8] c"Out\00", align 1
10+
11+
define i32 @CSMain() local_unnamed_addr {
12+
; CHECK-LABEL: define i32 @CSMain() local_unnamed_addr {
13+
; CHECK-NEXT: [[ENTRY:.*:]]
14+
; CHECK-NEXT: [[LOADGLOBAL:%.*]] = load i32, ptr @CBV, align 4
15+
; CHECK-NEXT: [[CMP_I1_NOT:%.*]] = icmp eq i32 [[LOADGLOBAL]], 0
16+
; CHECK-NEXT: br i1 [[CMP_I1_NOT]], label %[[CSMAIN_EXIT:.*]], label %[[FOR_BODY_I_LR_PH:.*]]
17+
; CHECK: [[FOR_BODY_I_LR_PH]]:
18+
; CHECK-NEXT: [[BUF:%.*]] = tail call target("dx.RawBuffer", i32, 1, 0) @llvm.dx.resource.handlefrombinding.tdx.RawBuffer_i32_1_0t(i32 0, i32 0, i32 4, i32 [[LOADGLOBAL]], ptr nonnull @Out.str)
19+
; CHECK-NEXT: br label %[[FOR_BODY_I:.*]]
20+
; CHECK: [[FOR_BODY_I]]:
21+
; CHECK-NEXT: [[LOOPPHI:%.*]] = phi i32 [ 0, %[[FOR_BODY_I_LR_PH]] ], [ [[INC_I:%.*]], %[[FOR_BODY_I]] ]
22+
; CHECK-NEXT: [[UPDATECNT:%.*]] = tail call noundef i32 @llvm.dx.resource.updatecounter.tdx.RawBuffer_i32_1_0t(target("dx.RawBuffer", i32, 1, 0) [[BUF]], i8 1)
23+
; CHECK-NEXT: [[INC_I]] = add nuw nsw i32 [[LOOPPHI]], 1
24+
; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[INC_I]], [[LOADGLOBAL]]
25+
; CHECK-NEXT: br i1 [[EXITCOND]], label %[[FOR_BODY_I]], label %[[CSMAIN_EXIT]]
26+
; CHECK: [[CSMAIN_EXIT]]:
27+
; CHECK-NEXT: [[BUFEXIT:%.*]] = tail call target("dx.RawBuffer", i32, 1, 0) @llvm.dx.resource.handlefrombinding.tdx.RawBuffer_i32_1_0t(i32 0, i32 0, i32 4, i32 [[LOADGLOBAL]], ptr nonnull @Out.str)
28+
; CHECK-NEXT: [[UPDATECNTEXIT:%.*]] = tail call noundef i32 @llvm.dx.resource.updatecounter.tdx.RawBuffer_i32_1_0t(target("dx.RawBuffer", i32, 1, 0) [[BUFEXIT]], i8 1)
29+
; CHECK-NEXT: ret i32 [[UPDATECNTEXIT]]
30+
;
31+
entry:
32+
%loadGlobal = load i32, ptr @CBV, align 4
33+
%cmp.i1.not = icmp eq i32 %loadGlobal, 0
34+
br i1 %cmp.i1.not, label %CSMain.exit, label %for.body.i.lr.ph
35+
36+
for.body.i.lr.ph:
37+
%buf = tail call target("dx.RawBuffer", i32, 1, 0) @llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 4, i32 %loadGlobal, ptr nonnull @Out.str)
38+
br label %for.body.i
39+
40+
for.body.i:
41+
%loopPhi = phi i32 [ 0, %for.body.i.lr.ph ], [ %inc.i, %for.body.i ]
42+
%updateCnt = tail call noundef i32 @llvm.dx.resource.updatecounter(target("dx.RawBuffer", i32, 1, 0) %buf, i8 1)
43+
%inc.i = add nuw nsw i32 %loopPhi, 1
44+
%exitcond = icmp ne i32 %inc.i, %loadGlobal
45+
br i1 %exitcond, label %for.body.i, label %CSMain.exit
46+
47+
CSMain.exit:
48+
%bufExit = tail call target("dx.RawBuffer", i32, 1, 0) @llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 4, i32 %loadGlobal, ptr nonnull @Out.str)
49+
%updateCntExit = tail call noundef i32 @llvm.dx.resource.updatecounter(target("dx.RawBuffer", i32, 1, 0) %bufExit, i8 1)
50+
ret i32 %updateCntExit
51+
}

0 commit comments

Comments
 (0)