Skip to content

Commit 4b4aaf1

Browse files
committed
[clang][Interp] Fix non-initializing CK_VectorSplat casts
Create the usual local variable to fill.
1 parent 6ec02f7 commit 4b4aaf1

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -488,14 +488,25 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
488488
if (DiscardResult)
489489
return this->discard(SubExpr);
490490

491-
assert(Initializing); // FIXME: Not always correct.
491+
if (!Initializing) {
492+
std::optional<unsigned> LocalIndex = allocateLocal(CE);
493+
if (!LocalIndex)
494+
return false;
495+
if (!this->emitGetPtrLocal(*LocalIndex, CE))
496+
return false;
497+
}
498+
492499
const auto *VT = CE->getType()->getAs<VectorType>();
493-
PrimType ElemT = classifyPrim(SubExpr);
500+
PrimType ElemT = classifyPrim(SubExpr->getType());
494501
unsigned ElemOffset = allocateLocalPrimitive(
495502
SubExpr, ElemT, /*IsConst=*/true, /*IsExtended=*/false);
496503

504+
// Prepare a local variable for the scalar value.
497505
if (!this->visit(SubExpr))
498506
return false;
507+
if (classifyPrim(SubExpr) == PT_Ptr && !this->emitLoadPop(ElemT, CE))
508+
return false;
509+
499510
if (!this->emitSetLocal(ElemT, ElemOffset, CE))
500511
return false;
501512

@@ -2778,6 +2789,7 @@ template <class Emitter>
27782789
bool ByteCodeExprGen<Emitter>::VisitExtVectorElementExpr(
27792790
const ExtVectorElementExpr *E) {
27802791
const Expr *Base = E->getBase();
2792+
assert(Base->getType()->isVectorType());
27812793

27822794
SmallVector<uint32_t, 4> Indices;
27832795
E->getEncodedElementAccess(Indices);

clang/test/AST/Interp/hlsl.hlsl

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
2+
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
3+
// RUN: -o - | FileCheck %s
4+
5+
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
6+
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
7+
// RUN: -o - -fexperimental-new-constant-interpreter | FileCheck %s
8+
9+
10+
/// This test converts V to a 1-element vector and then .xx to a 2-element vector.
11+
// CHECK-LABEL: ToTwoInts
12+
// CHECK: [[splat:%.*]] = insertelement <1 x i32> poison, i32 {{.*}}, i64 0
13+
// CHECK: [[vec2:%.*]] = shufflevector <1 x i32> [[splat]], <1 x i32> poison, <2 x i32> zeroinitializer
14+
// CHECK: ret <2 x i32> [[vec2]]
15+
int2 ToTwoInts(int V){
16+
return V.xx;
17+
}
18+
19+

0 commit comments

Comments
 (0)