[clang] 4b4aaf1 - [clang][Interp] Fix non-initializing CK_VectorSplat casts

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 18 00:05:42 PDT 2024


Author: Timm Bäder
Date: 2024-06-18T09:05:28+02:00
New Revision: 4b4aaf1e792367b4ce0e24966a7d21e2a83bb979

URL: https://github.com/llvm/llvm-project/commit/4b4aaf1e792367b4ce0e24966a7d21e2a83bb979
DIFF: https://github.com/llvm/llvm-project/commit/4b4aaf1e792367b4ce0e24966a7d21e2a83bb979.diff

LOG: [clang][Interp] Fix non-initializing CK_VectorSplat casts

Create the usual local variable to fill.

Added: 
    clang/test/AST/Interp/hlsl.hlsl

Modified: 
    clang/lib/AST/Interp/ByteCodeExprGen.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index b5e27bfb1a6db..0964ac046e630 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -488,14 +488,25 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
     if (DiscardResult)
       return this->discard(SubExpr);
 
-    assert(Initializing); // FIXME: Not always correct.
+    if (!Initializing) {
+      std::optional<unsigned> LocalIndex = allocateLocal(CE);
+      if (!LocalIndex)
+        return false;
+      if (!this->emitGetPtrLocal(*LocalIndex, CE))
+        return false;
+    }
+
     const auto *VT = CE->getType()->getAs<VectorType>();
-    PrimType ElemT = classifyPrim(SubExpr);
+    PrimType ElemT = classifyPrim(SubExpr->getType());
     unsigned ElemOffset = allocateLocalPrimitive(
         SubExpr, ElemT, /*IsConst=*/true, /*IsExtended=*/false);
 
+    // Prepare a local variable for the scalar value.
     if (!this->visit(SubExpr))
       return false;
+    if (classifyPrim(SubExpr) == PT_Ptr && !this->emitLoadPop(ElemT, CE))
+      return false;
+
     if (!this->emitSetLocal(ElemT, ElemOffset, CE))
       return false;
 
@@ -2778,6 +2789,7 @@ template <class Emitter>
 bool ByteCodeExprGen<Emitter>::VisitExtVectorElementExpr(
     const ExtVectorElementExpr *E) {
   const Expr *Base = E->getBase();
+  assert(Base->getType()->isVectorType());
 
   SmallVector<uint32_t, 4> Indices;
   E->getEncodedElementAccess(Indices);

diff  --git a/clang/test/AST/Interp/hlsl.hlsl b/clang/test/AST/Interp/hlsl.hlsl
new file mode 100644
index 0000000000000..cb14662c11f39
--- /dev/null
+++ b/clang/test/AST/Interp/hlsl.hlsl
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
+// RUN:   dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
+// RUN:   -o - | FileCheck %s
+
+// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
+// RUN:   dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
+// RUN:   -o - -fexperimental-new-constant-interpreter | FileCheck %s
+
+
+/// This test converts V to a 1-element vector and then .xx to a 2-element vector.
+// CHECK-LABEL: ToTwoInts
+// CHECK: [[splat:%.*]] = insertelement <1 x i32> poison, i32 {{.*}}, i64 0
+// CHECK: [[vec2:%.*]] = shufflevector <1 x i32> [[splat]], <1 x i32> poison, <2 x i32> zeroinitializer
+// CHECK: ret <2 x i32> [[vec2]]
+int2 ToTwoInts(int V){
+  return V.xx;
+}
+
+


        


More information about the cfe-commits mailing list