[clang] fbf0a80 - [clang][bytecode] Implement HLSLVectorTruncation casts (#108499)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 13 02:32:16 PDT 2024
Author: Timm Baeder
Date: 2024-09-13T11:32:12+02:00
New Revision: fbf0a8015389bccab80bba00be49955079913152
URL: https://github.com/llvm/llvm-project/commit/fbf0a8015389bccab80bba00be49955079913152
DIFF: https://github.com/llvm/llvm-project/commit/fbf0a8015389bccab80bba00be49955079913152.diff
LOG: [clang][bytecode] Implement HLSLVectorTruncation casts (#108499)
Added:
Modified:
clang/lib/AST/ByteCode/Compiler.cpp
clang/test/AST/ByteCode/hlsl.hlsl
Removed:
################################################################################
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