Skip to content

Commit ef58fa1

Browse files
authored
Fix const var init with NRVO (#3803)
1 parent ad4a3ea commit ef58fa1

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

tools/clang/lib/CodeGen/CGDecl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ void CodeGenFunction::EmitVarDecl(const VarDecl &D) {
142142
}
143143
// HLSL Change Begin - treat local constant as static.
144144
// Global variable will be generated instead of alloca.
145-
if (D.getType().isConstQualified() && D.isLocalVarDecl()) {
145+
if (D.getType().isConstQualified() &&
146+
(D.isLocalVarDecl() && D.getKind() != Decl::ParmVar &&
147+
!D.isNRVOVariable())) {
146148
// Only create global when has constant init.
147149
if (!isTrivialInitializer(D.getInit()) && CGM.EmitConstantInit(D, this)) {
148150
llvm::GlobalValue::LinkageTypes Linkage =
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %dxc -E main -T vs_6_0 %s | FileCheck %s
2+
3+
// Make sure NRVO works with constant initializer for return value
4+
// CHECK: call void @dx.op.storeOutput.f32(i32 5, i32 0, i32 0, i8 0, float 2.500000e-01)
5+
// CHECK: call void @dx.op.storeOutput.f32(i32 5, i32 0, i32 0, i8 1, float 5.000000e-01)
6+
// CHECK: call void @dx.op.storeOutput.f32(i32 5, i32 0, i32 0, i8 2, float 7.500000e-01)
7+
// CHECK: call void @dx.op.storeOutput.f32(i32 5, i32 0, i32 0, i8 3, float 1.000000e+00)
8+
9+
struct VSOUT {
10+
float4 pos : SV_Position;
11+
};
12+
13+
VSOUT main() {
14+
const VSOUT Out = {float4(0.25, 0.5, 0.75, 1.0)};
15+
return Out;
16+
}

0 commit comments

Comments
 (0)