[clang] [clang][bytecode] Implement HLSLVectorTruncation casts (PR #108499)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 12 22:48:29 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/108499.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+25)
- (modified) clang/test/AST/ByteCode/hlsl.hlsl (+14-2)
``````````diff
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!");
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/108499
More information about the cfe-commits
mailing list