[clang] [clang][bytecode] Implement HLSLVectorTruncation casts (PR #108499)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 12 22:47:59 PDT 2024


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/108499

None

>From 058053a85bb2fe41b2bb98c2e460df1f74058f63 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 13 Sep 2024 06:21:25 +0200
Subject: [PATCH] [clang][bytecode] Implement HLSLVectorTruncation casts

---
 clang/lib/AST/ByteCode/Compiler.cpp | 25 +++++++++++++++++++++++++
 clang/test/AST/ByteCode/hlsl.hlsl   | 16 ++++++++++++++--
 2 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 265350e44d95b9..5247b8cadf8d4f 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -644,6 +644,31 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
     return true;
   }
 
+  case CK_HLSLVectorTruncation: {
+    assert(SubExpr->getType()->isVectorType());
+    if (std::optional<PrimType> ResultT = classify(CE)) {
+      assert(!DiscardResult);
+      // Result must be either a float or integer. Take the first element.
+      if (!this->visit(SubExpr))
+        return false;
+      return this->emitArrayElemPop(*ResultT, 0, CE);
+    }
+    // Otherwise, this truncates from one vector type to another.
+    assert(CE->getType()->isVectorType());
+
+    if (!Initializing) {
+      unsigned LocalIndex = allocateTemporary(CE);
+      if (!this->emitGetPtrLocal(LocalIndex, CE))
+        return false;
+    }
+    unsigned ToSize = CE->getType()->getAs<VectorType>()->getNumElements();
+    assert(SubExpr->getType()->getAs<VectorType>()->getNumElements() > ToSize);
+    if (!this->visit(SubExpr))
+      return false;
+    return this->emitCopyArray(classifyVectorElementType(CE->getType()), 0, 0,
+                               ToSize, CE);
+  };
+
   case CK_ToVoid:
     return discard(SubExpr);
 
diff --git a/clang/test/AST/ByteCode/hlsl.hlsl b/clang/test/AST/ByteCode/hlsl.hlsl
index cb14662c11f394..073e4301919914 100644
--- a/clang/test/AST/ByteCode/hlsl.hlsl
+++ b/clang/test/AST/ByteCode/hlsl.hlsl
@@ -1,8 +1,8 @@
-// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
+// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -std=hlsl202x -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: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -std=hlsl202x -triple \
 // RUN:   dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
 // RUN:   -o - -fexperimental-new-constant-interpreter | FileCheck %s
 
@@ -16,4 +16,16 @@ int2 ToTwoInts(int V){
   return V.xx;
 }
 
+export void fn() {
+  // This compiling successfully verifies that the vector constant expression
+  // gets truncated to an integer at compile time for instantiation.
+  _Static_assert(((int)1.xxxx) + 0 == 1, "Woo!");
 
+  // This compiling successfully verifies that the vector constant expression
+  // gets truncated to a float at compile time for instantiation.
+  _Static_assert(((float)1.0.xxxx) + 0.0 == 1.0, "Woo!");
+
+  // This compiling successfully verifies that a vector can be truncated to a
+  // smaller vector, then truncated to a float as a constant expression.
+  _Static_assert(((float2)float4(6, 5, 4, 3)).x == 6, "Woo!");
+}



More information about the cfe-commits mailing list