| 1 |
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -disable-llvm-passes -emit-llvm -finclude-default-header -o - %s | FileCheck %s --check-prefixes=CHECK,ALL |
| 2 |
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -O3 -emit-llvm -finclude-default-header -o - %s | FileCheck %s --check-prefixes=OPT,ALL |
| 3 |
|
| 4 |
// Case 1: Simple floating integral conversion. |
| 5 |
// In this test case a float value is passed to an inout parameter taking an |
| 6 |
// integer. It is converted to an integer on call and converted back after the |
| 7 |
// function. |
| 8 |
|
| 9 |
// CHECK: define hidden void {{.*}}trunc_Param{{.*}}(ptr noalias noundef nonnull align 4 dereferenceable(4) {{%.*}}) |
| 10 |
void trunc_Param(inout int X) {} |
| 11 |
|
| 12 |
// ALL-LABEL: define noundef nofpclass(nan inf) float {{.*}}case1 |
| 13 |
// CHECK: [[F:%.*]] = alloca float |
| 14 |
// CHECK: [[ArgTmp:%.*]] = alloca i32 |
| 15 |
// CHECK: [[FVal:%.*]] = load float, ptr {{.*}} |
| 16 |
// CHECK: [[IVal:%.*]] = fptosi float [[FVal]] to i32 |
| 17 |
// CHECK: store i32 [[IVal]], ptr [[ArgTmp]] |
| 18 |
// CHECK: call void {{.*}}trunc_Param{{.*}}(ptr noalias noundef nonnull align 4 dereferenceable(4) [[ArgTmp]]) |
| 19 |
// CHECK: [[IRet:%.*]] = load i32, ptr [[ArgTmp]] |
| 20 |
// CHECK: [[FRet:%.*]] = sitofp i32 [[IRet]] to float |
| 21 |
// CHECK: store float [[FRet]], ptr [[F]] |
| 22 |
// OPT: [[IVal:%.*]] = fptosi float {{.*}} to i32 |
| 23 |
// OPT: [[FVal:%.*]] = sitofp i32 [[IVal]] to float |
| 24 |
// OPT: ret float [[FVal]] |
| 25 |
export float case1(float F) { |
| 26 |
trunc_Param(F); |
| 27 |
return F; |
| 28 |
} |
| 29 |
|
| 30 |
// Case 2: Uninitialized `out` parameters. |
| 31 |
// `out` parameters are not pre-initialized by the caller, so they are |
| 32 |
// uninitialized in the function. If they are not initialized before the |
| 33 |
// function returns the value is undefined. |
| 34 |
|
| 35 |
// CHECK: define hidden void {{.*}}undef{{.*}}(ptr noalias noundef nonnull align 4 dereferenceable(4) {{%.*}}) |
| 36 |
void undef(out int Z) { } |
| 37 |
|
| 38 |
// ALL-LABEL: define noundef i32 {{.*}}case2 |
| 39 |
// CHECK: [[V:%.*]] = alloca i32 |
| 40 |
// CHECK: [[ArgTmp:%.*]] = alloca i32 |
| 41 |
// CHECK-NOT: store {{.*}}, ptr [[ArgTmp]] |
| 42 |
// CHECK: call void {{.*}}unde{{.*}}(ptr noalias noundef nonnull align 4 dereferenceable(4) [[ArgTmp]]) |
| 43 |
// CHECK-NOT: store {{.*}}, ptr [[ArgTmp]] |
| 44 |
// CHECK: [[Res:%.*]] = load i32, ptr [[ArgTmp]] |
| 45 |
// CHECK: store i32 [[Res]], ptr [[V]], align 4 |
| 46 |
// OPT: ret i32 undef |
| 47 |
export int case2() { |
| 48 |
int V; |
| 49 |
undef(V); |
| 50 |
return V; |
| 51 |
} |
| 52 |
|
| 53 |
// Case 3: Simple initialized `out` parameter. |
| 54 |
// This test should verify that an out parameter value is written to as |
| 55 |
// expected. |
| 56 |
|
| 57 |
// CHECK: define hidden void {{.*}}zero{{.*}}(ptr noalias noundef nonnull align 4 dereferenceable(4) {{%.*}}) |
| 58 |
void zero(out int Z) { Z = 0; } |
| 59 |
|
| 60 |
// ALL-LABEL: define noundef i32 {{.*}}case3 |
| 61 |
// CHECK: [[V:%.*]] = alloca i32 |
| 62 |
// CHECK: [[ArgTmp:%.*]] = alloca i32 |
| 63 |
// CHECK-NOT: store {{.*}}, ptr [[ArgTmp]] |
| 64 |
// CHECK: call void {{.*}}zero{{.*}}(ptr noalias noundef nonnull align 4 dereferenceable(4) [[ArgTmp]]) |
| 65 |
// CHECK-NOT: store {{.*}}, ptr [[ArgTmp]] |
| 66 |
// CHECK: [[Res:%.*]] = load i32, ptr [[ArgTmp]] |
| 67 |
// CHECK: store i32 [[Res]], ptr [[V]], align 4 |
| 68 |
// OPT: ret i32 0 |
| 69 |
export int case3() { |
| 70 |
int V; |
| 71 |
zero(V); |
| 72 |
return V; |
| 73 |
} |
| 74 |
|
| 75 |
// Case 4: Vector swizzle arguments. |
| 76 |
// Vector swizzles in HLSL produce lvalues, so they can be used as arguments to |
| 77 |
// inout parameters and the swizzle is reversed on writeback. |
| 78 |
|
| 79 |
// CHECK: define hidden void {{.*}}funky{{.*}}(ptr noalias noundef nonnull align 16 dereferenceable(16) {{%.*}}) |
| 80 |
void funky(inout int3 X) { |
| 81 |
X.x += 1; |
| 82 |
X.y += 2; |
| 83 |
X.z += 3; |
| 84 |
} |
| 85 |
|
| 86 |
// ALL-LABEL: define noundef {{.*}}<3 x i32> {{.*}}case4 |
| 87 |
|
| 88 |
// This block initializes V = 0.xxx. |
| 89 |
// CHECK: [[V:%.*]] = alloca <3 x i32> |
| 90 |
// CHECK: [[ArgTmp:%.*]] = alloca <3 x i32> |
| 91 |
// CHECK: store <1 x i32> zeroinitializer, ptr [[ZeroPtr:%.*]] |
| 92 |
// CHECK: [[ZeroV1:%.*]] = load <1 x i32>, ptr [[ZeroPtr]] |
| 93 |
// CHECK: [[ZeroV3:%.*]] = shufflevector <1 x i32> [[ZeroV1]], <1 x i32> poison, <3 x i32> zeroinitializer |
| 94 |
// CHECK: store <3 x i32> [[ZeroV3]], ptr [[V]] |
| 95 |
|
| 96 |
// Shuffle the vector to the temporary. |
| 97 |
// CHECK: [[VVal:%.*]] = load <3 x i32>, ptr [[V]] |
| 98 |
// CHECK: [[Vyzx:%.*]] = shufflevector <3 x i32> [[VVal]], <3 x i32> poison, <3 x i32> <i32 1, i32 2, i32 0> |
| 99 |
// CHECK: store <3 x i32> [[Vyzx]], ptr [[ArgTmp]] |
| 100 |
|
| 101 |
// Call the function with the temporary. |
| 102 |
// CHECK: call void {{.*}}funky{{.*}}(ptr noalias noundef nonnull align 16 dereferenceable(16) [[ArgTmp]]) |
| 103 |
|
| 104 |
// Write it back. |
| 105 |
// CHECK: [[RetVal:%.*]] = load <3 x i32>, ptr [[ArgTmp]] |
| 106 |
// CHECK: [[Src0:%.*]] = extractelement <3 x i32> [[RetVal]], i32 0 |
| 107 |
// CHECK: [[PtrY:%.*]] = getelementptr <3 x i32>, ptr %V, i32 0, i32 1 |
| 108 |
// CHECK: store i32 [[Src0]], ptr [[PtrY]], align 4 |
| 109 |
// CHECK: [[Src1:%.*]] = extractelement <3 x i32> [[RetVal]], i32 1 |
| 110 |
// CHECK: [[PtrZ:%.*]] = getelementptr <3 x i32>, ptr %V, i32 0, i32 2 |
| 111 |
// CHECK: store i32 [[Src1]], ptr [[PtrZ]], align 4 |
| 112 |
// CHECK: [[Src2:%.*]] = extractelement <3 x i32> [[RetVal]], i32 2 |
| 113 |
// CHECK: store i32 [[Src2]], ptr %V, align 4 |
| 114 |
|
| 115 |
// OPT: ret <3 x i32> <i32 3, i32 1, i32 2> |
| 116 |
export int3 case4() { |
| 117 |
int3 V = 0.xxx; |
| 118 |
funky(V.yzx); |
| 119 |
return V; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
// Case 5: Straightforward inout of a scalar value. |
| 124 |
|
| 125 |
// CHECK: define hidden void {{.*}}increment{{.*}}(ptr noalias noundef nonnull align 4 dereferenceable(4) {{%.*}}) |
| 126 |
void increment(inout int I) { |
| 127 |
I += 1; |
| 128 |
} |
| 129 |
|
| 130 |
// ALL-LABEL: define noundef i32 {{.*}}case5 |
| 131 |
|
| 132 |
// CHECK: [[I:%.*]] = alloca i32 |
| 133 |
// CHECK: [[ArgTmp:%.*]] = alloca i32 |
| 134 |
// CHECK: store i32 4, ptr [[I]] |
| 135 |
// CHECK: [[IInit:%.*]] = load i32, ptr [[I]] |
| 136 |
// CHECK: store i32 [[IInit:%.*]], ptr [[ArgTmp]], align 4 |
| 137 |
// CHECK: call void {{.*}}increment{{.*}}(ptr noalias noundef nonnull align 4 dereferenceable(4) [[ArgTmp]]) |
| 138 |
// CHECK: [[RetVal:%.*]] = load i32, ptr [[ArgTmp]] |
| 139 |
// CHECK: store i32 [[RetVal]], ptr [[I]], align 4 |
| 140 |
// OPT: ret i32 5 |
| 141 |
export int case5() { |
| 142 |
int I = 4; |
| 143 |
increment(I); |
| 144 |
return I; |
| 145 |
} |
| 146 |
|
| 147 |
// Case 6: Aggregate out parameters. |
| 148 |
struct S { |
| 149 |
int X; |
| 150 |
float Y; |
| 151 |
}; |
| 152 |
|
| 153 |
// CHECK: define hidden void {{.*}}init{{.*}}(ptr noalias noundef nonnull align 1 dereferenceable(8) {{%.*}}) |
| 154 |
void init(out S s) { |
| 155 |
s.X = 3; |
| 156 |
s.Y = 4; |
| 157 |
} |
| 158 |
|
| 159 |
// ALL-LABEL: define noundef i32 {{.*}}case6 |
| 160 |
|
| 161 |
// CHECK: [[S:%.*]] = alloca %struct.S |
| 162 |
// CHECK: [[Tmp:%.*]] = alloca %struct.S |
| 163 |
// CHECK: call void {{.*}}init{{.*}}(ptr noalias noundef nonnull align 1 dereferenceable(8) [[Tmp]]) |
| 164 |
// CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 1 [[S]], ptr align 1 [[Tmp]], i32 8, i1 false) |
| 165 |
|
| 166 |
// OPT: ret i32 7 |
| 167 |
export int case6() { |
| 168 |
S s; |
| 169 |
init(s); |
| 170 |
return s.X + s.Y; |
| 171 |
} |
| 172 |
|
| 173 |
// Case 7: Aggregate inout parameters. |
| 174 |
struct R { |
| 175 |
int X; |
| 176 |
float Y; |
| 177 |
}; |
| 178 |
|
| 179 |
// CHECK: define hidden void {{.*}}init{{.*}}(ptr noalias noundef nonnull align 1 dereferenceable(8) {{%.*}}) |
| 180 |
void init(inout R s) { |
| 181 |
s.X = 3; |
| 182 |
s.Y = 4; |
| 183 |
} |
| 184 |
|
| 185 |
// ALL-LABEL: define noundef i32 {{.*}}case7 |
| 186 |
|
| 187 |
// CHECK: [[S:%.*]] = alloca %struct.R |
| 188 |
// CHECK: [[Tmp:%.*]] = alloca %struct.R |
| 189 |
// CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 1 [[Tmp]], ptr align 1 [[S]], i32 8, i1 false) |
| 190 |
// CHECK: call void {{.*}}init{{.*}}(ptr noalias noundef nonnull align 1 dereferenceable(8) [[Tmp]]) |
| 191 |
// CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 1 [[S]], ptr align 1 [[Tmp]], i32 8, i1 false) |
| 192 |
|
| 193 |
// OPT: ret i32 7 |
| 194 |
export int case7() { |
| 195 |
R s; |
| 196 |
init(s); |
| 197 |
return s.X + s.Y; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
// Case 8: Non-scalars with a cast expression. |
| 202 |
|
| 203 |
// CHECK: define hidden void {{.*}}trunc_vec{{.*}}(ptr noalias noundef nonnull align 16 dereferenceable(16) {{%.*}}) |
| 204 |
void trunc_vec(inout int3 V) {} |
| 205 |
|
| 206 |
// ALL-LABEL: define noundef nofpclass(nan inf) <3 x float> {{.*}}case8 |
| 207 |
|
| 208 |
// CHECK: [[V:%.*]] = alloca <3 x float> |
| 209 |
// CHECK: [[Tmp:%.*]] = alloca <3 x i32> |
| 210 |
// CHECK: [[FVal:%.*]] = load <3 x float>, ptr [[V]] |
| 211 |
// CHECK: [[IVal:%.*]] = fptosi <3 x float> [[FVal]] to <3 x i32> |
| 212 |
// CHECK: store <3 x i32> [[IVal]], ptr [[Tmp]] |
| 213 |
// CHECK: call void {{.*}}trunc_vec{{.*}}(ptr noalias noundef nonnull align 16 dereferenceable(16) [[Tmp]]) |
| 214 |
// CHECK: [[IRet:%.*]] = load <3 x i32>, ptr [[Tmp]] |
| 215 |
// CHECK: [[FRet:%.*]] = sitofp <3 x i32> [[IRet]] to <3 x float> |
| 216 |
// CHECK: store <3 x float> [[FRet]], ptr [[V]] |
| 217 |
|
| 218 |
// OPT: [[IVal:%.*]] = fptosi <3 x float> {{.*}} to <3 x i32> |
| 219 |
// OPT: [[FVal:%.*]] = sitofp <3 x i32> [[IVal]] to <3 x float> |
| 220 |
// OPT: ret <3 x float> [[FVal]] |
| 221 |
|
| 222 |
export float3 case8(float3 V) { |
| 223 |
trunc_vec(V); |
| 224 |
return V; |
| 225 |
} |
| 226 |
|
| 227 |
// Case 9: Side-effecting lvalue argument expression! |
| 228 |
|
| 229 |
void do_nothing(inout int V) {} |
| 230 |
|
| 231 |
// ALL-LABEL: define noundef i32 {{.*}}case9 |
| 232 |
// CHECK: [[V:%.*]] = alloca i32 |
| 233 |
// CHECK: [[Tmp:%.*]] = alloca i32 |
| 234 |
// CHECK: store i32 0, ptr [[V]] |
| 235 |
// CHECK: [[VVal:%.*]] = load i32, ptr [[V]] |
| 236 |
// CHECK: [[VInc:%.*]] = add nsw i32 [[VVal]], 1 |
| 237 |
// CHECK: store i32 [[VInc]], ptr [[V]] |
| 238 |
// CHECK: [[VArg:%.*]] = load i32, ptr [[V]] |
| 239 |
// CHECK-NOT: add |
| 240 |
// CHECK: store i32 [[VArg]], ptr [[Tmp]] |
| 241 |
// CHECK: call void {{.*}}do_nothing{{.*}}(ptr noalias noundef nonnull align 4 dereferenceable(4) [[Tmp]]) |
| 242 |
// CHECK: [[RetVal:%.*]] = load i32, ptr [[Tmp]] |
| 243 |
// CHECK: store i32 [[RetVal]], ptr [[V]] |
| 244 |
|
| 245 |
// OPT: ret i32 1 |
| 246 |
export int case9() { |
| 247 |
int V = 0; |
| 248 |
do_nothing(++V); |
| 249 |
return V; |
| 250 |
} |
| 251 |
|
| 252 |
// Case 10: Verify argument writeback ordering for aliasing arguments. |
| 253 |
|
| 254 |
void order_matters(inout int X, inout int Y) { |
| 255 |
Y = 2; |
| 256 |
X = 1; |
| 257 |
} |
| 258 |
|
| 259 |
// ALL-LABEL: define noundef i32 {{.*}}case10 |
| 260 |
|
| 261 |
// CHECK: [[V:%.*]] = alloca i32 |
| 262 |
// CHECK: [[Tmp0:%.*]] = alloca i32 |
| 263 |
// CHECK: [[Tmp1:%.*]] = alloca i32 |
| 264 |
// CHECK: store i32 0, ptr [[V]] |
| 265 |
// CHECK: [[VVal:%.*]] = load i32, ptr [[V]] |
| 266 |
// CHECK: store i32 [[VVal]], ptr [[Tmp0]] |
| 267 |
// CHECK: [[VVal:%.*]] = load i32, ptr [[V]] |
| 268 |
// CHECK: store i32 [[VVal]], ptr [[Tmp1]] |
| 269 |
// CHECK: call void {{.*}}order_matters{{.*}}(ptr noalias noundef nonnull align 4 dereferenceable(4) [[Tmp0]], ptr noalias noundef nonnull align 4 dereferenceable(4) [[Tmp1]]) |
| 270 |
// CHECK: [[Arg1Val:%.*]] = load i32, ptr [[Tmp0]] |
| 271 |
// CHECK: store i32 [[Arg1Val]], ptr [[V]] |
| 272 |
// CHECK: [[Arg2Val:%.*]] = load i32, ptr [[Tmp1]] |
| 273 |
// CHECK: store i32 [[Arg2Val]], ptr [[V]] |
| 274 |
|
| 275 |
// OPT: ret i32 2 |
| 276 |
export int case10() { |
| 277 |
int V = 0; |
| 278 |
order_matters(V, V); |
| 279 |
return V; |
| 280 |
} |
| 281 |
|
| 282 |
// Case 11: Verify inout on bitfield lvalues |
| 283 |
|
| 284 |
struct B { |
| 285 |
int X : 8; |
| 286 |
int Y : 8; |
| 287 |
}; |
| 288 |
|
| 289 |
void setFour(inout int I) { |
| 290 |
I = 4; |
| 291 |
} |
| 292 |
|
| 293 |
// ALL-LABEL: define {{.*}} i32 {{.*}}case11 |
| 294 |
|
| 295 |
// CHECK: [[B:%.*]] = alloca %struct.B |
| 296 |
// CHECK: [[Tmp:%.*]] = alloca i32 |
| 297 |
|
| 298 |
// CHECK: [[BFLoad:%.*]] = load i8, ptr [[B]] |
| 299 |
// CHECK: [[BFcast:%.*]] = sext i8 [[BFLoad]] to i32 |
| 300 |
// CHECK: store i32 [[BFcast]], ptr [[Tmp]] |
| 301 |
// CHECK: call void {{.*}}setFour{{.*}}(ptr noalias noundef nonnull align 4 dereferenceable(4) [[Tmp]]) |
| 302 |
// CHECK: [[RetVal:%.*]] = load i32, ptr [[Tmp]] |
| 303 |
// CHECK: [[TruncVal:%.*]] = trunc i32 [[RetVal]] to i8 |
| 304 |
// CHECK: store i8 [[TruncVal]], ptr [[B]] |
| 305 |
|
| 306 |
// OPT: ret i32 8 |
| 307 |
export int case11() { |
| 308 |
B b = {1 , 2}; |
| 309 |
setFour(b.X); |
| 310 |
return b.X * b.Y; |
| 311 |
} |
| 312 |
|
| 313 |
// Case 12: Uninitialized out parameters are undefined |
| 314 |
|
| 315 |
void oops(out int X) {} |
| 316 |
// ALL-LABEL: define {{.*}} i32 {{.*}}case12 |
| 317 |
|
| 318 |
// CHECK: [[V:%.*]] = alloca i32 |
| 319 |
// CHECK: [[Tmp:%.*]] = alloca i32 |
| 320 |
// CHECK-NOT: store {{.*}}, ptr [[Tmp]] |
| 321 |
// CHECK: call void {{.*}}oops{{.*}}(ptr noalias noundef nonnull align 4 dereferenceable(4) [[Tmp]]) |
| 322 |
// CHECK: [[ArgVal:%.*]] = load i32, ptr [[Tmp]] |
| 323 |
// CHECK: store i32 [[ArgVal]], ptr [[V]] |
| 324 |
|
| 325 |
// OPT: ret i32 undef |
| 326 |
export int case12() { |
| 327 |
int V = 0; |
| 328 |
oops(V); |
| 329 |
return V; |
| 330 |
} |