Skip to content

Commit 085d0b0

Browse files
authored
[CIR] Add verifier for vtable initializer (llvm#155031)
This adds verification for the initializer, if present, of any global passed to the `cir.vtable.address_point` op.
1 parent 480d528 commit 085d0b0

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ struct MissingFeatures {
273273
static bool thunks() { return false; }
274274
static bool tryEmitAsConstant() { return false; }
275275
static bool typeChecks() { return false; }
276-
static bool vtableInitializer() { return false; }
277276
static bool weakRefReference() { return false; }
278277
static bool writebacks() { return false; }
279278
static bool appleKext() { return false; }

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,15 +1459,18 @@ cir::VTableAddrPointOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
14591459
StringRef name = getName();
14601460

14611461
// Verify that the result type underlying pointer type matches the type of
1462-
// the referenced cir.global or cir.func op.
1463-
auto op = symbolTable.lookupNearestSymbolFrom<GlobalOp>(*this, getNameAttr());
1462+
// the referenced cir.global.
1463+
auto op =
1464+
symbolTable.lookupNearestSymbolFrom<cir::GlobalOp>(*this, getNameAttr());
14641465
if (!op)
14651466
return emitOpError("'")
14661467
<< name << "' does not reference a valid cir.global";
14671468
std::optional<mlir::Attribute> init = op.getInitialValue();
14681469
if (!init)
14691470
return success();
1470-
assert(!cir::MissingFeatures::vtableInitializer());
1471+
if (!isa<cir::VTableAttr>(*init))
1472+
return emitOpError("Expected #cir.vtable in initializer for global '")
1473+
<< name << "'";
14711474
return success();
14721475
}
14731476

clang/test/CIR/IR/invalid-vtable.cir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ cir.func @reference_unknown_vtable() {
1010

1111
// -----
1212

13+
!u8i = !cir.int<u, 8>
14+
!u32i = !cir.int<u, 32>
15+
cir.global linkonce_odr @_ZTT1D = #cir.const_array<[#cir.global_view<@_ZTV1D, [0 : i32, 3 : i32]> : !cir.ptr<!u8i>, #cir.global_view<@_ZTC1D0_1B, [0 : i32, 3 : i32]> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 2>
16+
cir.func @reference_unknown_vtable() {
17+
// expected-error @below {{Expected #cir.vtable in initializer for global '_ZTT1D'}}
18+
%0 = cir.vtable.address_point(@_ZTT1D, address_point = <index = 0, offset = 2>) : !cir.vptr
19+
cir.return
20+
}
21+
22+
// -----
23+
1324
!rec_S = !cir.record<struct "S" {!cir.vptr}>
1425
!u8i = !cir.int<u, 8>
1526
!rec_anon_struct = !cir.record<struct {!cir.array<!cir.ptr<!u8i> x 4>}>

0 commit comments

Comments
 (0)