Skip to content

Commit 72e42cb

Browse files
sitio-coutolanza
authored andcommitted
[CIR][Bugfix] Fix vtableAttr const struct usage
PR #200 broke the vtableAttr usage, as it was not properly refactored to use ArrayAttr instead of ConstStructAttr to store its members.
1 parent c50e16d commit 72e42cb

File tree

5 files changed

+20
-14
lines changed

5 files changed

+20
-14
lines changed

clang/include/clang/CIR/Dialect/IR/CIRAttrs.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,18 +347,18 @@ def VTableAttr : CIR_Attr<"VTable", "vtable", [TypedAttrInterface]> {
347347
// `vtable_data` is const struct with one element, containing an array of
348348
// vtable information.
349349
let parameters = (ins AttributeSelfTypeParameter<"">:$type,
350-
"ConstStructAttr":$vtable_data);
350+
"ArrayAttr":$vtable_data);
351351

352352
let builders = [
353353
AttrBuilderWithInferredContext<(ins "Type":$type,
354-
"ConstStructAttr":$vtable_data), [{
354+
"ArrayAttr":$vtable_data), [{
355355
return $_get(type.getContext(), type, vtable_data);
356356
}]>
357357
];
358358

359359
let genVerifyDecl = 1;
360360
let assemblyFormat = [{
361-
`<` $vtable_data `>`
361+
`<` custom<StructMembers>($vtable_data) `>`
362362
}];
363363
}
364364

clang/lib/CIR/CodeGen/ConstantInitBuilder.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,9 @@ class ConstantAggregateBuilderTemplateBase
406406
assert(initCSA &&
407407
"expected #cir.const_struct attribute to represent vtable data");
408408
return this->Builder.setGlobalInitializer(
409-
global, forVTable
410-
? mlir::cir::VTableAttr::get(initCSA.getType(), initCSA)
411-
: init);
409+
global, forVTable ? mlir::cir::VTableAttr::get(initCSA.getType(),
410+
initCSA.getMembers())
411+
: init);
412412
}
413413

414414
/// Given that this builder was created by beginning an array or struct

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,28 +2091,25 @@ LogicalResult TypeInfoAttr::verify(
20912091

20922092
LogicalResult
20932093
VTableAttr::verify(::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError,
2094-
::mlir::Type type, ConstStructAttr vtableData) {
2094+
::mlir::Type type, ::mlir::ArrayAttr vtableData) {
20952095
auto sTy = type.dyn_cast_or_null<mlir::cir::StructType>();
20962096
if (!sTy) {
20972097
emitError() << "expected !cir.struct type result";
20982098
return failure();
20992099
}
2100-
if (sTy.getMembers().size() != 1 || vtableData.getMembers().size() != 1) {
2100+
if (sTy.getMembers().size() != 1 || vtableData.size() != 1) {
21012101
emitError() << "expected struct type with only one subtype";
21022102
return failure();
21032103
}
21042104

21052105
auto arrayTy = sTy.getMembers()[0].dyn_cast<mlir::cir::ArrayType>();
2106-
auto constArrayAttr =
2107-
vtableData.getMembers()[0].dyn_cast<mlir::cir::ConstArrayAttr>();
2106+
auto constArrayAttr = vtableData[0].dyn_cast<mlir::cir::ConstArrayAttr>();
21082107
if (!arrayTy || !constArrayAttr) {
21092108
emitError() << "expected struct type with one array element";
21102109
return failure();
21112110
}
21122111

2113-
if (mlir::cir::ConstStructAttr::verify(emitError, type,
2114-
vtableData.getMembers())
2115-
.failed())
2112+
if (mlir::cir::ConstStructAttr::verify(emitError, type, vtableData).failed())
21162113
return failure();
21172114

21182115
LogicalResult eltTypeCheck = success();

clang/test/CIR/CodeGen/vtable-rtti.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class B : public A
7373
// CHECK: }
7474

7575
// vtable for B
76-
// CHECK: cir.global linkonce_odr @_ZTV1B = #cir.vtable<<{#cir.const_array<[#cir.null : !cir.ptr<!u8i>, #cir.global_view<@_ZTI1B> : !cir.ptr<!u8i>, #cir.global_view<@_ZN1BD2Ev> : !cir.ptr<!u8i>, #cir.global_view<@_ZN1BD0Ev> : !cir.ptr<!u8i>, #cir.global_view<@_ZNK1A5quackEv> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 5>}>> : ![[VTableTypeA]]
76+
// CHECK: cir.global linkonce_odr @_ZTV1B = #cir.vtable<{#cir.const_array<[#cir.null : !cir.ptr<!u8i>, #cir.global_view<@_ZTI1B> : !cir.ptr<!u8i>, #cir.global_view<@_ZN1BD2Ev> : !cir.ptr<!u8i>, #cir.global_view<@_ZN1BD0Ev> : !cir.ptr<!u8i>, #cir.global_view<@_ZNK1A5quackEv> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 5>}> : ![[VTableTypeA]]
7777

7878
// vtable for __cxxabiv1::__si_class_type_info
7979
// CHECK: cir.global "private" external @_ZTVN10__cxxabiv120__si_class_type_infoE : !cir.ptr<!cir.ptr<!u8i>>

clang/test/CIR/IR/vtableAttr.cir

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: cir-opt %s | FileCheck %s
2+
3+
!u8i = !cir.int<u, 8>
4+
!ty_2222 = !cir.struct<"", !cir.array<!cir.ptr<!u8i> x 1>>
5+
module {
6+
// Should parse VTable attribute.
7+
cir.global external @testVTable = #cir.vtable<{#cir.const_array<[#cir.null : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 1>}> : !ty_2222
8+
// CHECK: cir.global external @testVTable = #cir.vtable<{#cir.const_array<[#cir.null : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 1>}> : !ty_2222
9+
}

0 commit comments

Comments
 (0)