diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td index 0e42de854351..c3281e30506c 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td @@ -918,42 +918,39 @@ def CIR_DynamicCastInfoAttr : CIR_Attr<"DynamicCastInfo", "dyn_cast_info"> { let description = [{ Provide ABI specific information about a dynamic cast operation. - The `srcRtti` and the `destRtti` parameters give the RTTI of the source + The `src_rtti` and the `dest_rtti` parameters give the RTTI of the source record type and the destination record type, respectively. - The `runtimeFunc` parameter gives the `__dynamic_cast` function which is - provided by the runtime. The `badCastFunc` parameter gives the + The `runtime_func` parameter gives the `__dynamic_cast` function which is + provided by the runtime. The `bad_cast_func` parameter gives the `__cxa_bad_cast` function which is also provided by the runtime. - The `offsetHint` parameter gives the hint value that should be passed to the - `__dynamic_cast` runtime function. + The `offset_hint` parameter gives the hint value that should be passed to + the `__dynamic_cast` runtime function. }]; let parameters = (ins - CIR_GlobalViewAttr:$srcRtti, - CIR_GlobalViewAttr:$destRtti, - "mlir::FlatSymbolRefAttr":$runtimeFunc, - "mlir::FlatSymbolRefAttr":$badCastFunc, - CIR_IntAttr:$offsetHint + CIR_GlobalViewAttr:$src_rtti, + CIR_GlobalViewAttr:$dest_rtti, + "mlir::FlatSymbolRefAttr":$runtime_func, + "mlir::FlatSymbolRefAttr":$bad_cast_func, + CIR_IntAttr:$offset_hint ); let builders = [ - AttrBuilderWithInferredContext<(ins "GlobalViewAttr":$srcRtti, - "GlobalViewAttr":$destRtti, - "mlir::FlatSymbolRefAttr":$runtimeFunc, - "mlir::FlatSymbolRefAttr":$badCastFunc, - "IntAttr":$offsetHint), [{ - return $_get(srcRtti.getContext(), srcRtti, destRtti, runtimeFunc, - badCastFunc, offsetHint); + AttrBuilderWithInferredContext<(ins "GlobalViewAttr":$src_rtti, + "GlobalViewAttr":$dest_rtti, + "mlir::FlatSymbolRefAttr":$runtime_func, + "mlir::FlatSymbolRefAttr":$bad_cast_func, + "IntAttr":$offset_hint), [{ + return $_get(src_rtti.getContext(), src_rtti, dest_rtti, runtime_func, + bad_cast_func, offset_hint); }]>, ]; let genVerifyDecl = 1; let assemblyFormat = [{ - `<` - qualified($srcRtti) `,` qualified($destRtti) `,` - $runtimeFunc `,` $badCastFunc `,` qualified($offsetHint) - `>` + `<` struct(qualified($src_rtti), qualified($dest_rtti), $runtime_func, $bad_cast_func, qualified($offset_hint)) `>` }]; let extraClassDeclaration = [{ diff --git a/clang/test/CIR/CodeGen/dynamic-cast.cpp b/clang/test/CIR/CodeGen/dynamic-cast.cpp index 0d2cae3317af..05a0d21413ef 100644 --- a/clang/test/CIR/CodeGen/dynamic-cast.cpp +++ b/clang/test/CIR/CodeGen/dynamic-cast.cpp @@ -9,7 +9,7 @@ struct Base { struct Derived : Base {}; -// BEFORE-DAG: #dyn_cast_info__ZTI4Base__ZTI7Derived = #cir.dyn_cast_info<#cir.global_view<@_ZTI4Base> : !cir.ptr, #cir.global_view<@_ZTI7Derived> : !cir.ptr, @__dynamic_cast, @__cxa_bad_cast, #cir.int<0> : !s64i> +// BEFORE-DAG: #dyn_cast_info__ZTI4Base__ZTI7Derived = #cir.dyn_cast_info : !cir.ptr, dest_rtti = #cir.global_view<@_ZTI7Derived> : !cir.ptr, runtime_func = @__dynamic_cast, bad_cast_func = @__cxa_bad_cast, offset_hint = #cir.int<0> : !s64i> // BEFORE-DAG: !rec_Base = !cir.record // BEFORE-DAG: !rec_Derived = !cir.record diff --git a/clang/test/CIR/IR/dynamic-cast.cir b/clang/test/CIR/IR/dynamic-cast.cir new file mode 100644 index 000000000000..283f11e25b82 --- /dev/null +++ b/clang/test/CIR/IR/dynamic-cast.cir @@ -0,0 +1,59 @@ +// RUN: cir-opt --verify-roundtrip %s | FileCheck %s + +!s64i = !cir.int +!u8i = !cir.int +!void = !cir.void + +!rec_Base = !cir.record +!rec_Derived = !cir.record + +#dyn_cast_info__ZTI4Base__ZTI7Derived = #cir.dyn_cast_info : !cir.ptr, dest_rtti = #cir.global_view<@_ZTI7Derived> : !cir.ptr, runtime_func = @__dynamic_cast, bad_cast_func = @__cxa_bad_cast, offset_hint = #cir.int<0> : !s64i> + +// CHECK: #dyn_cast_info__ZTI4Base__ZTI7Derived = #cir.dyn_cast_info : !cir.ptr, dest_rtti = #cir.global_view<@_ZTI7Derived> : !cir.ptr, runtime_func = @__dynamic_cast, bad_cast_func = @__cxa_bad_cast, offset_hint = #cir.int<0> : !s64i> + +module { + cir.global "private" constant external @_ZTI4Base : !cir.ptr + cir.global "private" constant external @_ZTI7Derived : !cir.ptr + cir.func private @__dynamic_cast(!cir.ptr, !cir.ptr, !cir.ptr, !s64i) -> !cir.ptr + cir.func private @__cxa_bad_cast() + + cir.func @test_ptr_cast(%arg0: !cir.ptr) -> !cir.ptr { + %0 = cir.dyn_cast ptr %arg0 : !cir.ptr -> !cir.ptr #dyn_cast_info__ZTI4Base__ZTI7Derived + cir.return %0 : !cir.ptr + } + + // CHECK: cir.func @test_ptr_cast(%arg0: !cir.ptr) -> !cir.ptr { + // CHECK: %0 = cir.dyn_cast ptr %arg0 : !cir.ptr -> !cir.ptr #dyn_cast_info__ZTI4Base__ZTI7Derived + // CHECK: cir.return %0 : !cir.ptr + // CHECK: } + + cir.func @test_ref_cast(%arg0: !cir.ptr) -> !cir.ptr { + %0 = cir.dyn_cast ref %arg0 : !cir.ptr -> !cir.ptr #dyn_cast_info__ZTI4Base__ZTI7Derived + cir.return %0 : !cir.ptr + } + + // CHECK: cir.func @test_ref_cast(%arg0: !cir.ptr) -> !cir.ptr { + // CHECK: %0 = cir.dyn_cast ref %arg0 : !cir.ptr -> !cir.ptr #dyn_cast_info__ZTI4Base__ZTI7Derived + // CHECK: cir.return %0 : !cir.ptr + // CHECK: } + + cir.func dso_local @test_cast_to_void(%arg0: !cir.ptr) -> !cir.ptr { + %0 = cir.dyn_cast ptr %arg0 : !cir.ptr -> !cir.ptr + cir.return %0 : !cir.ptr + } + + // CHECK: cir.func dso_local @test_cast_to_void(%arg0: !cir.ptr) -> !cir.ptr { + // CHECK: %0 = cir.dyn_cast ptr %arg0 : !cir.ptr -> !cir.ptr + // CHECK: cir.return %0 : !cir.ptr + // CHECK: } + + cir.func dso_local @test_relative_layout_cast(%arg0: !cir.ptr) -> !cir.ptr { + %0 = cir.dyn_cast ptr relative_layout %arg0 : !cir.ptr -> !cir.ptr + cir.return %0 : !cir.ptr + } + + // CHECK: cir.func dso_local @test_relative_layout_cast(%arg0: !cir.ptr) -> !cir.ptr { + // CHECK: %0 = cir.dyn_cast ptr relative_layout %arg0 : !cir.ptr -> !cir.ptr + // CHECK: cir.return %0 : !cir.ptr + // CHECK: } +} diff --git a/clang/test/CIR/IR/invalid.cir b/clang/test/CIR/IR/invalid.cir index b1b42a9d7505..83cacb4a653c 100644 --- a/clang/test/CIR/IR/invalid.cir +++ b/clang/test/CIR/IR/invalid.cir @@ -1192,7 +1192,7 @@ module { cir.func private @__cxa_bad_cast() cir.func @test(%arg0 : !cir.ptr) { // expected-error@+1 {{srcRtti must be an RTTI pointer}} - %0 = cir.dyn_cast ptr %arg0 : !cir.ptr -> !cir.ptr #cir.dyn_cast_info<#cir.global_view<@_ZTI4Base> : !cir.ptr, #cir.global_view<@_ZTI7Derived> : !cir.ptr, @__dynamic_cast, @__cxa_bad_cast, #cir.int<0> : !s64i> + %0 = cir.dyn_cast ptr %arg0 : !cir.ptr -> !cir.ptr #cir.dyn_cast_info : !cir.ptr, dest_rtti = #cir.global_view<@_ZTI7Derived> : !cir.ptr, runtime_func = @__dynamic_cast, bad_cast_func = @__cxa_bad_cast, offset_hint = #cir.int<0> : !s64i> } } @@ -1214,7 +1214,7 @@ module { cir.func private @__cxa_bad_cast() cir.func @test(%arg0 : !cir.ptr) { // expected-error@+1 {{destRtti must be an RTTI pointer}} - %0 = cir.dyn_cast ptr %arg0 : !cir.ptr -> !cir.ptr #cir.dyn_cast_info<#cir.global_view<@_ZTI4Base> : !cir.ptr, #cir.global_view<@_ZTI7Derived> : !cir.ptr, @__dynamic_cast, @__cxa_bad_cast, #cir.int<0> : !s64i> + %0 = cir.dyn_cast ptr %arg0 : !cir.ptr -> !cir.ptr #cir.dyn_cast_info : !cir.ptr, dest_rtti = #cir.global_view<@_ZTI7Derived> : !cir.ptr, runtime_func = @__dynamic_cast, bad_cast_func = @__cxa_bad_cast, offset_hint = #cir.int<0> : !s64i> } }