1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
//===- llvm.c - Test of llvm APIs -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM
// Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// RUN: mlir-capi-llvm-test 2>&1 | FileCheck %s
#include "mlir-c/Dialect/LLVM.h"
#include "mlir-c/BuiltinTypes.h"
#include "mlir-c/IR.h"
#include "mlir-c/Support.h"
#include <assert.h>
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// CHECK-LABEL: testTypeCreation()
static void testTypeCreation(MlirContext ctx) {
fprintf(stderr, "testTypeCreation()\n");
MlirType i8 = mlirIntegerTypeGet(ctx, 8);
MlirType i32 = mlirIntegerTypeGet(ctx, 32);
MlirType i64 = mlirIntegerTypeGet(ctx, 64);
const char *ptr_text = "!llvm.ptr";
MlirType ptr = mlirLLVMPointerTypeGet(ctx, 0);
MlirType ptr_ref =
mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(ptr_text));
// CHECK: !llvm.ptr: 1
fprintf(stderr, "%s: %d\n", ptr_text, mlirTypeEqual(ptr, ptr_ref));
const char *ptr_addr_text = "!llvm.ptr<42>";
MlirType ptr_addr = mlirLLVMPointerTypeGet(ctx, 42);
MlirType ptr_addr_ref =
mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(ptr_addr_text));
// CHECK: !llvm.ptr<42>: 1
fprintf(stderr, "%s: %d\n", ptr_addr_text,
mlirTypeEqual(ptr_addr, ptr_addr_ref));
const char *voidt_text = "!llvm.void";
MlirType voidt = mlirLLVMVoidTypeGet(ctx);
MlirType voidt_ref =
mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(voidt_text));
// CHECK: !llvm.void: 1
fprintf(stderr, "%s: %d\n", voidt_text, mlirTypeEqual(voidt, voidt_ref));
const char *i32_4_text = "!llvm.array<4 x i32>";
MlirType i32_4 = mlirLLVMArrayTypeGet(i32, 4);
MlirType i32_4_ref =
mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(i32_4_text));
// CHECK: !llvm.array<4 x i32>: 1
fprintf(stderr, "%s: %d\n", i32_4_text, mlirTypeEqual(i32_4, i32_4_ref));
const char *i8_i32_i64_text = "!llvm.func<i8 (i32, i64)>";
const MlirType i32_i64_arr[] = {i32, i64};
MlirType i8_i32_i64 = mlirLLVMFunctionTypeGet(i8, 2, i32_i64_arr, false);
MlirType i8_i32_i64_ref =
mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(i8_i32_i64_text));
// CHECK: !llvm.func<i8 (i32, i64)>: 1
fprintf(stderr, "%s: %d\n", i8_i32_i64_text,
mlirTypeEqual(i8_i32_i64, i8_i32_i64_ref));
const char *i32_i64_s_text = "!llvm.struct<(i32, i64)>";
MlirType i32_i64_s = mlirLLVMStructTypeLiteralGet(ctx, 2, i32_i64_arr, false);
MlirType i32_i64_s_ref =
mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(i32_i64_s_text));
// CHECK: !llvm.struct<(i32, i64)>: 1
fprintf(stderr, "%s: %d\n", i32_i64_s_text,
mlirTypeEqual(i32_i64_s, i32_i64_s_ref));
}
// CHECK-LABEL: testStructTypeCreation
static int testStructTypeCreation(MlirContext ctx) {
fprintf(stderr, "testStructTypeCreation");
// CHECK: !llvm.struct<()>
mlirTypeDump(mlirLLVMStructTypeLiteralGet(ctx, /*nFieldTypes=*/0,
/*fieldTypes=*/NULL,
/*isPacked=*/false));
MlirType i8 = mlirIntegerTypeGet(ctx, 8);
MlirType i32 = mlirIntegerTypeGet(ctx, 32);
MlirType i64 = mlirIntegerTypeGet(ctx, 64);
MlirType i8_i32_i64[] = {i8, i32, i64};
// CHECK: !llvm.struct<(i8, i32, i64)>
mlirTypeDump(
mlirLLVMStructTypeLiteralGet(ctx, sizeof(i8_i32_i64) / sizeof(MlirType),
i8_i32_i64, /*isPacked=*/false));
// CHECK: !llvm.struct<(i32)>
mlirTypeDump(mlirLLVMStructTypeLiteralGet(ctx, 1, &i32, /*isPacked=*/false));
MlirType i32_i32[] = {i32, i32};
// CHECK: !llvm.struct<packed (i32, i32)>
mlirTypeDump(mlirLLVMStructTypeLiteralGet(
ctx, sizeof(i32_i32) / sizeof(MlirType), i32_i32, /*isPacked=*/true));
MlirType literal =
mlirLLVMStructTypeLiteralGet(ctx, sizeof(i8_i32_i64) / sizeof(MlirType),
i8_i32_i64, /*isPacked=*/false);
// CHECK: num elements: 3
// CHECK: i8
// CHECK: i32
// CHECK: i64
fprintf(stderr, "num elements: %" PRIdPTR "\n",
mlirLLVMStructTypeGetNumElementTypes(literal));
for (intptr_t i = 0; i < 3; ++i) {
mlirTypeDump(mlirLLVMStructTypeGetElementType(literal, i));
}
if (!mlirTypeEqual(
mlirLLVMStructTypeLiteralGet(ctx, 1, &i32, /*isPacked=*/false),
mlirLLVMStructTypeLiteralGet(ctx, 1, &i32, /*isPacked=*/false))) {
return 1;
}
if (mlirTypeEqual(
mlirLLVMStructTypeLiteralGet(ctx, 1, &i32, /*isPacked=*/false),
mlirLLVMStructTypeLiteralGet(ctx, 1, &i64, /*isPacked=*/false))) {
return 2;
}
// CHECK: !llvm.struct<"foo", opaque>
// CHECK: !llvm.struct<"bar", opaque>
mlirTypeDump(mlirLLVMStructTypeIdentifiedGet(
ctx, mlirStringRefCreateFromCString("foo")));
mlirTypeDump(mlirLLVMStructTypeIdentifiedGet(
ctx, mlirStringRefCreateFromCString("bar")));
if (!mlirTypeEqual(mlirLLVMStructTypeIdentifiedGet(
ctx, mlirStringRefCreateFromCString("foo")),
mlirLLVMStructTypeIdentifiedGet(
ctx, mlirStringRefCreateFromCString("foo")))) {
return 3;
}
if (mlirTypeEqual(mlirLLVMStructTypeIdentifiedGet(
ctx, mlirStringRefCreateFromCString("foo")),
mlirLLVMStructTypeIdentifiedGet(
ctx, mlirStringRefCreateFromCString("bar")))) {
return 4;
}
MlirType fooStruct = mlirLLVMStructTypeIdentifiedGet(
ctx, mlirStringRefCreateFromCString("foo"));
MlirStringRef name = mlirLLVMStructTypeGetIdentifier(fooStruct);
if (memcmp(name.data, "foo", name.length))
return 5;
if (!mlirLLVMStructTypeIsOpaque(fooStruct))
return 6;
MlirType i32_i64[] = {i32, i64};
MlirLogicalResult result =
mlirLLVMStructTypeSetBody(fooStruct, sizeof(i32_i64) / sizeof(MlirType),
i32_i64, /*isPacked=*/false);
if (!mlirLogicalResultIsSuccess(result))
return 7;
// CHECK: !llvm.struct<"foo", (i32, i64)>
mlirTypeDump(fooStruct);
if (mlirLLVMStructTypeIsOpaque(fooStruct))
return 8;
if (mlirLLVMStructTypeIsPacked(fooStruct))
return 9;
if (!mlirTypeEqual(mlirLLVMStructTypeIdentifiedGet(
ctx, mlirStringRefCreateFromCString("foo")),
fooStruct)) {
return 10;
}
MlirType barStruct = mlirLLVMStructTypeIdentifiedGet(
ctx, mlirStringRefCreateFromCString("bar"));
result = mlirLLVMStructTypeSetBody(barStruct, 1, &i32, /*isPacked=*/true);
if (!mlirLogicalResultIsSuccess(result))
return 11;
// CHECK: !llvm.struct<"bar", packed (i32)>
mlirTypeDump(barStruct);
if (!mlirLLVMStructTypeIsPacked(barStruct))
return 12;
// Same body, should succeed.
result =
mlirLLVMStructTypeSetBody(fooStruct, sizeof(i32_i64) / sizeof(MlirType),
i32_i64, /*isPacked=*/false);
if (!mlirLogicalResultIsSuccess(result))
return 13;
// Different body, should fail.
result = mlirLLVMStructTypeSetBody(fooStruct, 1, &i32, /*isPacked=*/false);
if (mlirLogicalResultIsSuccess(result))
return 14;
// Packed flag differs, should fail.
result = mlirLLVMStructTypeSetBody(barStruct, 1, &i32, /*isPacked=*/false);
if (mlirLogicalResultIsSuccess(result))
return 15;
// Should have a different name.
// CHECK: !llvm.struct<"foo{{[^"]+}}
mlirTypeDump(mlirLLVMStructTypeIdentifiedNewGet(
ctx, mlirStringRefCreateFromCString("foo"), /*nFieldTypes=*/0,
/*fieldTypes=*/NULL, /*isPacked=*/false));
// Two freshly created "new" types must differ.
if (mlirTypeEqual(
mlirLLVMStructTypeIdentifiedNewGet(
ctx, mlirStringRefCreateFromCString("foo"), /*nFieldTypes=*/0,
/*fieldTypes=*/NULL, /*isPacked=*/false),
mlirLLVMStructTypeIdentifiedNewGet(
ctx, mlirStringRefCreateFromCString("foo"), /*nFieldTypes=*/0,
/*fieldTypes=*/NULL, /*isPacked=*/false))) {
return 16;
}
MlirType opaque = mlirLLVMStructTypeOpaqueGet(
ctx, mlirStringRefCreateFromCString("opaque"));
// CHECK: !llvm.struct<"opaque", opaque>
mlirTypeDump(opaque);
if (!mlirLLVMStructTypeIsOpaque(opaque))
return 17;
return 0;
}
int main(void) {
MlirContext ctx = mlirContextCreate();
mlirDialectHandleRegisterDialect(mlirGetDialectHandle__llvm__(), ctx);
mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("llvm"));
testTypeCreation(ctx);
int result = testStructTypeCreation(ctx);
mlirContextDestroy(ctx);
if (result)
fprintf(stderr, "FAILED: code %d", result);
return result;
}
|