[llvm] [DirectX backend] avoid generate redundant bitcast in DXILPrepareModule (PR #65163)

Xiang Li via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 1 16:07:59 PDT 2023


https://github.com/python3kgae updated https://github.com/llvm/llvm-project/pull/65163:

>From 774579639e320df70b2150fc526a726876d79859 Mon Sep 17 00:00:00 2001
From: Xiang Li <python3kgae at outlook.com>
Date: Thu, 31 Aug 2023 18:44:06 -0400
Subject: [PATCH 1/2] [DirectX backend] avoid generate redundant bitcast in
 DXILPrepareModule

When emit NoOp bitcast for GEP Ptr Operand, should use SourceElementType instead of ResultElementType.
---
 llvm/lib/Target/DirectX/DXILPrepare.cpp | 2 +-
 llvm/test/CodeGen/DirectX/typed_ptr.ll  | 9 ++++++++-
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/DirectX/DXILPrepare.cpp b/llvm/lib/Target/DirectX/DXILPrepare.cpp
index 300924017c89f88..026911946b47f00 100644
--- a/llvm/lib/Target/DirectX/DXILPrepare.cpp
+++ b/llvm/lib/Target/DirectX/DXILPrepare.cpp
@@ -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;
           }
diff --git a/llvm/test/CodeGen/DirectX/typed_ptr.ll b/llvm/test/CodeGen/DirectX/typed_ptr.ll
index 5a9610cf87af4a9..79ec7c4af6d5f8a 100644
--- a/llvm/test/CodeGen/DirectX/typed_ptr.ll
+++ b/llvm/test/CodeGen/DirectX/typed_ptr.ll
@@ -2,9 +2,16 @@
 target triple = "dxil-unknown-unknown"
 
 ; Make sure not crash when has typed ptr.
-; CHECK:@test
+; CHECK:define i64 @test(ptr %p)
+; Make sure no bitcast generated.
+; CHECK-NOT:bitcast
+
+ at gs = external addrspace(3) global [20 x [6 x float]], align 4
 
 define i64 @test(i64* %p) {
+  %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
   %v = load i64, i64* %p
   ret i64 %v
 }

>From 3c31869ec8d3570e36aca0dacc3d467a4cae1c87 Mon Sep 17 00:00:00 2001
From: Xiang Li <python3kgae at outlook.com>
Date: Fri, 1 Sep 2023 19:07:38 -0400
Subject: [PATCH 2/2] Cleanup test per comments.

---
 llvm/test/CodeGen/DirectX/typed_ptr.ll | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/llvm/test/CodeGen/DirectX/typed_ptr.ll b/llvm/test/CodeGen/DirectX/typed_ptr.ll
index 79ec7c4af6d5f8a..2975d85b9c96442 100644
--- a/llvm/test/CodeGen/DirectX/typed_ptr.ll
+++ b/llvm/test/CodeGen/DirectX/typed_ptr.ll
@@ -1,17 +1,29 @@
 ; RUN: opt -S -dxil-prepare < %s | FileCheck %s
 target triple = "dxil-unknown-unknown"
 
-; Make sure not crash when has typed ptr.
-; CHECK:define i64 @test(ptr %p)
-; Make sure no bitcast generated.
-; CHECK-NOT:bitcast
-
 @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
-  %v = load i64, i64* %p
-  ret i64 %v
+  ret void
 }



More information about the llvm-commits mailing list