Skip to content

Commit 1a5e18a

Browse files
authored
[HLSL] Do not print details in IR for target extension types (#115971)
This PR changes how target extension types are printed when they are emitted as IR. This prevents repetitive phrases like "struct = type {...}" from being repeated over and over in the outputted IR. Additionally, it should allow opt to not crash when parsing the DXIL output. Fixes [#114131](#114131)
1 parent d300337 commit 1a5e18a

File tree

7 files changed

+42
-7
lines changed

7 files changed

+42
-7
lines changed

clang/test/CodeGenHLSL/builtins/AppendStructuredBuffer-elementtype.hlsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ struct MyStruct {
1818
// DXIL: %"class.hlsl::AppendStructuredBuffer.9" = type { target("dx.RawBuffer", <3 x i32>, 1, 0)
1919
// DXIL: %"class.hlsl::AppendStructuredBuffer.10" = type { target("dx.RawBuffer", <2 x half>, 1, 0)
2020
// DXIL: %"class.hlsl::AppendStructuredBuffer.11" = type { target("dx.RawBuffer", <3 x float>, 1, 0)
21-
// DXIL: %"class.hlsl::AppendStructuredBuffer.12" = type { target("dx.RawBuffer", %struct.MyStruct = type { <4 x float>, <2 x i32>, [8 x i8] }, 1, 0)
21+
// DXIL: %"class.hlsl::AppendStructuredBuffer.12" = type { target("dx.RawBuffer", %struct.MyStruct, 1, 0)
22+
// DXIL: %struct.MyStruct = type { <4 x float>, <2 x i32>, [8 x i8] }
2223

2324
AppendStructuredBuffer<int16_t> BufI16;
2425
AppendStructuredBuffer<uint16_t> BufU16;

clang/test/CodeGenHLSL/builtins/ConsumeStructuredBuffer-elementtype.hlsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ struct MyStruct {
1818
// DXIL: %"class.hlsl::ConsumeStructuredBuffer.9" = type { target("dx.RawBuffer", <3 x i32>, 1, 0)
1919
// DXIL: %"class.hlsl::ConsumeStructuredBuffer.10" = type { target("dx.RawBuffer", <2 x half>, 1, 0)
2020
// DXIL: %"class.hlsl::ConsumeStructuredBuffer.11" = type { target("dx.RawBuffer", <3 x float>, 1, 0)
21-
// DXIL: %"class.hlsl::ConsumeStructuredBuffer.12" = type { target("dx.RawBuffer", %struct.MyStruct = type { <4 x float>, <2 x i32>, [8 x i8] }, 1, 0)
21+
// DXIL: %"class.hlsl::ConsumeStructuredBuffer.12" = type { target("dx.RawBuffer", %struct.MyStruct, 1, 0)
22+
// DXIL: %struct.MyStruct = type { <4 x float>, <2 x i32>, [8 x i8] }
2223

2324
ConsumeStructuredBuffer<int16_t> BufI16;
2425
ConsumeStructuredBuffer<uint16_t> BufU16;

clang/test/CodeGenHLSL/builtins/RasterizerOrderedStructuredBuffer-elementtype.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct MyStruct {
1818
// DXIL: %"class.hlsl::RasterizerOrderedStructuredBuffer.9" = type { target("dx.RawBuffer", <3 x i32>, 1, 1) }
1919
// DXIL: %"class.hlsl::RasterizerOrderedStructuredBuffer.10" = type { target("dx.RawBuffer", <2 x half>, 1, 1) }
2020
// DXIL: %"class.hlsl::RasterizerOrderedStructuredBuffer.11" = type { target("dx.RawBuffer", <3 x float>, 1, 1) }
21-
// DXIL: %"class.hlsl::RasterizerOrderedStructuredBuffer.12" = type { target("dx.RawBuffer", %struct.MyStruct = type { <4 x float>, <2 x i32>, [8 x i8] }, 1, 1) }
21+
// DXIL: %struct.MyStruct = type { <4 x float>, <2 x i32>, [8 x i8] }
2222

2323
RasterizerOrderedStructuredBuffer<int16_t> BufI16;
2424
RasterizerOrderedStructuredBuffer<uint16_t> BufU16;

clang/test/CodeGenHLSL/builtins/hlsl_resource_t.hlsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
using handle_float_t = __hlsl_resource_t [[hlsl::resource_class(UAV)]] [[hlsl::contained_type(float)]];
44

55
// CHECK: %"class.hlsl::RWBuffer" = type { target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
6-
// CHECK: %"class.hlsl::StructuredBuffer" = type { target("dx.RawBuffer", %struct.MyStruct = type { <4 x float>, <2 x i32>, [8 x i8] }, 0, 0)
6+
// CHECK: %"class.hlsl::StructuredBuffer" = type { target("dx.RawBuffer", %struct.MyStruct, 0, 0)
7+
// CHECK: %struct.MyStruct = type { <4 x float>, <2 x i32>, [8 x i8] }
78

89
// CHECK: define void @_Z2faU9_Res_u_CTfu17__hlsl_resource_t(target("dx.TypedBuffer", float, 1, 0, 0) %a)
910
// CHECK: call void @_Z4foo1U9_Res_u_CTfu17__hlsl_resource_t(target("dx.TypedBuffer", float, 1, 0, 0) %0)

clang/test/CodeGenHLSL/resource-bindings.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ struct S {
1515
int i;
1616
};
1717

18-
// CHECK: %T3S0_h = call target("dx.RawBuffer", %struct.S = type { <4 x float>, i32, [12 x i8] }, 0, 0) @llvm.dx.handle.fromBinding.tdx.RawBuffer_s_struct.Ss_0_0t(i32 0, i32 3, i32 1, i32 0, i1 false)
18+
// CHECK: %T3S0_h = call target("dx.RawBuffer", %struct.S, 0, 0) @llvm.dx.handle.fromBinding.tdx.RawBuffer_s_struct.Ss_0_0t(i32 0, i32 3, i32 1, i32 0, i1 false)
1919
StructuredBuffer<S> T3S0 : register(t3);

llvm/lib/IR/AsmWriter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,10 @@ void TypePrinting::print(Type *Ty, raw_ostream &OS) {
649649
OS << "target(\"";
650650
printEscapedString(Ty->getTargetExtName(), OS);
651651
OS << "\"";
652-
for (Type *Inner : TETy->type_params())
653-
OS << ", " << *Inner;
652+
for (Type *Inner : TETy->type_params()) {
653+
OS << ", ";
654+
Inner->print(OS, /*IsForDebug=*/false, /*NoDetails=*/true);
655+
}
654656
for (unsigned IntParam : TETy->int_params())
655657
OS << ", " << IntParam;
656658
OS << ")";

llvm/unittests/IR/TypesTest.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,40 @@ TEST(TypesTest, TargetExtType) {
4040
Type *A = TargetExtType::get(Context, "typea");
4141
Type *Aparam = TargetExtType::get(Context, "typea", {}, {0, 1});
4242
Type *Aparam2 = TargetExtType::get(Context, "typea", {}, {0, 1});
43+
4344
// Opaque types with same parameters are identical...
4445
EXPECT_EQ(Aparam, Aparam2);
4546
// ... but just having the same name is not enough.
4647
EXPECT_NE(A, Aparam);
48+
49+
// ensure struct types in targest extension types
50+
// only show the struct name, not the struct body
51+
Type *Int32Type = Type::getInt32Ty(Context);
52+
Type *FloatType = Type::getFloatTy(Context);
53+
std::array<Type *, 2> Elements = {Int32Type, FloatType};
54+
55+
StructType *Struct = llvm::StructType::create(Context, Elements, "MyStruct",
56+
/*isPacked=*/false);
57+
SmallVector<char, 50> TETV;
58+
llvm::raw_svector_ostream TETStream(TETV);
59+
Type *TargetExtensionType =
60+
TargetExtType::get(Context, "structTET", {Struct}, {0, 1});
61+
TargetExtensionType->print(TETStream);
62+
63+
EXPECT_STREQ(TETStream.str().str().data(),
64+
"target(\"structTET\", %MyStruct, 0, 1)");
65+
66+
// ensure that literal structs in the target extension type print the struct
67+
// body
68+
Struct = StructType::get(Context, Struct->elements(), /*isPacked=*/false);
69+
70+
TargetExtensionType =
71+
TargetExtType::get(Context, "structTET", {Struct}, {0, 1});
72+
TETV.clear();
73+
TargetExtensionType->print(TETStream);
74+
75+
EXPECT_STREQ(TETStream.str().str().data(),
76+
"target(\"structTET\", { i32, float }, 0, 1)");
4777
}
4878

4979
TEST(TypedPointerType, PrintTest) {

0 commit comments

Comments
 (0)