Skip to content

Commit

Permalink
[DirectX backend] avoid generate redundant bitcast in DXILPrepareModu…
Browse files Browse the repository at this point in the history
…le (#65163)

When emit NoOp bitcast for GEP Ptr Operand, should use SourceElementType
instead of ResultElementType.

**Behavior Before Change**
Redundant bitcast like 
   ` bitcast ptr addrspace(3) @gs to ptr addrspace(3)`
 will be generated for llvm/test/CodeGen/DirectX/typed_ptr.ll

**Behavior After Change**
  No bitcast will be generated.

Fixes #65183
  • Loading branch information
python3kgae committed Sep 2, 2023
1 parent 69d79f1 commit c21cd16
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion llvm/lib/Target/DirectX/DXILPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class DXILPrepareModule : public ModulePass {
if (auto GEP = dyn_cast<GetElementPtrInst>(&I)) {
if (Value *NoOpBitcast = maybeGenerateBitcast(
Builder, PointerTypes, I, GEP->getPointerOperand(),
GEP->getResultElementType()))
GEP->getSourceElementType()))
GEP->setOperand(0, NoOpBitcast);
continue;
}
Expand Down
23 changes: 21 additions & 2 deletions llvm/test/CodeGen/DirectX/typed_ptr.ll
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
; RUN: opt -S -dxil-prepare < %s | FileCheck %s
target triple = "dxil-unknown-unknown"

; Make sure not crash when has typed ptr.
; CHECK:@test
@gs = external addrspace(3) global [20 x [6 x float]], align 4

; Make sure not crash when has typed ptr.
define i64 @test(i64* %p) {
; CHECK-LABEL: define i64 @test(
; CHECK-SAME: ptr [[P:%.*]]) {
; CHECK-NEXT: [[V:%.*]] = load i64, ptr [[P]], align 4
; CHECK-NEXT: ret i64 [[V]]
;
%v = load i64, i64* %p
ret i64 %v
}

; Make sure no bitcast generated.
define void @test_gep() {
; CHECK-LABEL: define void @test_gep() {
; CHECK-NEXT: [[BASE:%.*]] = getelementptr inbounds [20 x [6 x float]], ptr addrspace(3) @gs, i64 0, i64 3
; CHECK-NEXT: [[ADDR:%.*]] = getelementptr inbounds [6 x float], ptr addrspace(3) [[BASE]], i64 0, i64 2
; CHECK-NEXT: store float 1.000000e+00, ptr addrspace(3) [[ADDR]], align 4
; CHECK-NEXT: ret void
;
%base = getelementptr inbounds [20 x [6 x float]], ptr addrspace(3) @gs, i64 0, i64 3
%addr = getelementptr inbounds [6 x float], ptr addrspace(3) %base, i64 0, i64 2
store float 1.000000e+00, ptr addrspace(3) %addr, align 4
ret void
}

0 comments on commit c21cd16

Please sign in to comment.