-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converts a union to a struct containing only its largest element. GetMemberOp for unions is lowered as bitcasts instead of GEPs, since union members share the same address space. ghstack-source-id: 791a944c5df3103c5671ba061fe4c7c3a56317fa Pull Request resolved: #230
- Loading branch information
1 parent
1eeb982
commit 57fa182
Showing
4 changed files
with
128 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// RUN: cir-opt %s -cir-to-llvm -o %t.mlir | ||
// RUN: FileCheck --input-file=%t.mlir %s | ||
|
||
!s16i = !cir.int<s, 16> | ||
!s32i = !cir.int<s, 32> | ||
#true = #cir.bool<true> : !cir.bool | ||
!ty_22U122 = !cir.struct<union "U1" {!cir.bool, !s16i, !s32i} #cir.recdecl.ast> | ||
!ty_22U222 = !cir.struct<union "U2" {f64, !ty_22U122} #cir.recdecl.ast> | ||
!ty_22U322 = !cir.struct<union "U3" {!s16i, !ty_22U122} #cir.recdecl.ast> | ||
module { | ||
// Should lower union to struct with only the largest member. | ||
cir.global external @u1 = #cir.zero : !ty_22U122 | ||
// CHECK: llvm.mlir.global external @u1() {addr_space = 0 : i32} : !llvm.struct<"union.U1", (i32)> | ||
|
||
// Should recursively find the largest member if there are nested unions. | ||
cir.global external @u2 = #cir.zero : !ty_22U222 | ||
cir.global external @u3 = #cir.zero : !ty_22U322 | ||
// CHECK: llvm.mlir.global external @u2() {addr_space = 0 : i32} : !llvm.struct<"union.U2", (f64)> | ||
// CHECK: llvm.mlir.global external @u3() {addr_space = 0 : i32} : !llvm.struct<"union.U3", (i32)> | ||
|
||
// CHECK: llvm.func @test | ||
cir.func @test(%arg0: !cir.ptr<!ty_22U122>) { | ||
|
||
// Should store directly to the union's base address. | ||
%5 = cir.const(#true) : !cir.bool | ||
%6 = cir.get_member %arg0[0] {name = "b"} : !cir.ptr<!ty_22U122> -> !cir.ptr<!cir.bool> | ||
cir.store %5, %6 : !cir.bool, cir.ptr <!cir.bool> | ||
// CHECK: %[[#VAL:]] = llvm.mlir.constant(1 : i8) : i8 | ||
// The bitcast it just to bypass the type checker. It will be replaced by an opaque pointer. | ||
// CHECK: %[[#ADDR:]] = llvm.bitcast %{{.+}} : !llvm.ptr<struct<"union.U1", (i32)>> to !llvm.ptr<i8> | ||
// CHECK: llvm.store %[[#VAL]], %[[#ADDR]] : !llvm.ptr<i8> | ||
|
||
// Should load direclty from the union's base address. | ||
%7 = cir.get_member %arg0[0] {name = "b"} : !cir.ptr<!ty_22U122> -> !cir.ptr<!cir.bool> | ||
%8 = cir.load %7 : cir.ptr <!cir.bool>, !cir.bool | ||
// The bitcast it just to bypass the type checker. It will be replaced by an opaque pointer. | ||
// CHECK: %[[#BASE:]] = llvm.bitcast %{{.+}} : !llvm.ptr<struct<"union.U1", (i32)>> to !llvm.ptr<i8> | ||
// CHECK: %{{.+}} = llvm.load %[[#BASE]] : !llvm.ptr<i8> | ||
|
||
cir.return | ||
} | ||
} |