Skip to content

Commit fbf0a80

Browse files
authored
[clang][bytecode] Implement HLSLVectorTruncation casts (llvm#108499)
1 parent e054712 commit fbf0a80

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,31 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
644644
return true;
645645
}
646646

647+
case CK_HLSLVectorTruncation: {
648+
assert(SubExpr->getType()->isVectorType());
649+
if (std::optional<PrimType> ResultT = classify(CE)) {
650+
assert(!DiscardResult);
651+
// Result must be either a float or integer. Take the first element.
652+
if (!this->visit(SubExpr))
653+
return false;
654+
return this->emitArrayElemPop(*ResultT, 0, CE);
655+
}
656+
// Otherwise, this truncates from one vector type to another.
657+
assert(CE->getType()->isVectorType());
658+
659+
if (!Initializing) {
660+
unsigned LocalIndex = allocateTemporary(CE);
661+
if (!this->emitGetPtrLocal(LocalIndex, CE))
662+
return false;
663+
}
664+
unsigned ToSize = CE->getType()->getAs<VectorType>()->getNumElements();
665+
assert(SubExpr->getType()->getAs<VectorType>()->getNumElements() > ToSize);
666+
if (!this->visit(SubExpr))
667+
return false;
668+
return this->emitCopyArray(classifyVectorElementType(CE->getType()), 0, 0,
669+
ToSize, CE);
670+
};
671+
647672
case CK_ToVoid:
648673
return discard(SubExpr);
649674

clang/test/AST/ByteCode/hlsl.hlsl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
1+
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -std=hlsl202x -triple \
22
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
33
// RUN: -o - | FileCheck %s
44

5-
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
5+
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -std=hlsl202x -triple \
66
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
77
// RUN: -o - -fexperimental-new-constant-interpreter | FileCheck %s
88

@@ -16,4 +16,16 @@ int2 ToTwoInts(int V){
1616
return V.xx;
1717
}
1818

19+
export void fn() {
20+
// This compiling successfully verifies that the vector constant expression
21+
// gets truncated to an integer at compile time for instantiation.
22+
_Static_assert(((int)1.xxxx) + 0 == 1, "Woo!");
1923

24+
// This compiling successfully verifies that the vector constant expression
25+
// gets truncated to a float at compile time for instantiation.
26+
_Static_assert(((float)1.0.xxxx) + 0.0 == 1.0, "Woo!");
27+
28+
// This compiling successfully verifies that a vector can be truncated to a
29+
// smaller vector, then truncated to a float as a constant expression.
30+
_Static_assert(((float2)float4(6, 5, 4, 3)).x == 6, "Woo!");
31+
}

0 commit comments

Comments
 (0)