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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Unit tests for CIR implementation of OpenACC's PointertLikeType interface
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/OpenACC/OpenACC.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/Value.h"
#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include "clang/CIR/Dialect/OpenACC/CIROpenACCTypeInterfaces.h"
#include "clang/CIR/Dialect/OpenACC/RegisterOpenACCExtensions.h"
#include "gtest/gtest.h"
using namespace mlir;
using namespace cir;
//===----------------------------------------------------------------------===//
// Test Fixture
//===----------------------------------------------------------------------===//
class CIROpenACCPointerLikeTest : public ::testing::Test {
protected:
CIROpenACCPointerLikeTest() : b(&context), loc(UnknownLoc::get(&context)) {
context.loadDialect<cir::CIRDialect>();
context.loadDialect<mlir::acc::OpenACCDialect>();
// Register extension to integrate CIR types with OpenACC.
mlir::DialectRegistry registry;
cir::acc::registerOpenACCExtensions(registry);
context.appendDialectRegistry(registry);
}
MLIRContext context;
OpBuilder b;
Location loc;
llvm::StringMap<unsigned> recordNames;
mlir::IntegerAttr getAlignOne(mlir::MLIRContext *ctx) {
// Note that mlir::IntegerType is used instead of cir::IntType here because
// we don't need sign information for this to be useful, so keep it simple.
clang::CharUnits align = clang::CharUnits::One();
return b.getI64IntegerAttr(align.getQuantity());
}
mlir::StringAttr getUniqueRecordName(const std::string &baseName) {
auto it = recordNames.find(baseName);
if (it == recordNames.end()) {
recordNames[baseName] = 0;
return b.getStringAttr(baseName);
}
return b.getStringAttr(baseName + "." +
std::to_string(recordNames[baseName]++));
}
// General handler for types without a specific test
void testSingleType(mlir::Type ty,
mlir::acc::VariableTypeCategory expectedTypeCategory) {
mlir::Type ptrTy = cir::PointerType::get(ty);
// cir::PointerType should be castable to acc::PointerLikeType
auto pltTy = dyn_cast_if_present<mlir::acc::PointerLikeType>(ptrTy);
ASSERT_NE(pltTy, nullptr);
EXPECT_EQ(pltTy.getElementType(), ty);
OwningOpRef<cir::AllocaOp> varPtrOp =
b.create<cir::AllocaOp>(loc, ptrTy, ty, "", getAlignOne(&context));
mlir::Value val = varPtrOp.get();
mlir::acc::VariableTypeCategory typeCategory = pltTy.getPointeeTypeCategory(
cast<TypedValue<mlir::acc::PointerLikeType>>(val),
mlir::acc::getVarType(varPtrOp.get()));
EXPECT_EQ(typeCategory, expectedTypeCategory);
}
void testScalarType(mlir::Type ty) {
testSingleType(ty, mlir::acc::VariableTypeCategory::scalar);
}
void testNonScalarType(mlir::Type ty) {
testSingleType(ty, mlir::acc::VariableTypeCategory::nonscalar);
}
void testUncategorizedType(mlir::Type ty) {
testSingleType(ty, mlir::acc::VariableTypeCategory::uncategorized);
}
void testArrayType(mlir::Type ty) {
// Build the array pointer type.
mlir::Type arrTy = cir::ArrayType::get(ty, 10);
mlir::Type ptrTy = cir::PointerType::get(arrTy);
// Verify that the pointer points to the array type..
auto pltTy = dyn_cast_if_present<mlir::acc::PointerLikeType>(ptrTy);
ASSERT_NE(pltTy, nullptr);
EXPECT_EQ(pltTy.getElementType(), arrTy);
// Create an alloca for the array
OwningOpRef<cir::AllocaOp> varPtrOp =
b.create<cir::AllocaOp>(loc, ptrTy, arrTy, "", getAlignOne(&context));
// Verify that the type category is array.
mlir::Value val = varPtrOp.get();
mlir::acc::VariableTypeCategory typeCategory = pltTy.getPointeeTypeCategory(
cast<TypedValue<mlir::acc::PointerLikeType>>(val),
mlir::acc::getVarType(varPtrOp.get()));
EXPECT_EQ(typeCategory, mlir::acc::VariableTypeCategory::array);
// Create an array-to-pointer decay cast.
mlir::Type ptrToElemTy = cir::PointerType::get(ty);
OwningOpRef<cir::CastOp> decayPtr = b.create<cir::CastOp>(
loc, ptrToElemTy, cir::CastKind::array_to_ptrdecay, val);
mlir::Value decayVal = decayPtr.get();
// Verify that we still get the expected element type.
auto decayPltTy =
dyn_cast_if_present<mlir::acc::PointerLikeType>(decayVal.getType());
ASSERT_NE(decayPltTy, nullptr);
EXPECT_EQ(decayPltTy.getElementType(), ty);
// Verify that we still identify the type category as an array.
mlir::acc::VariableTypeCategory decayTypeCategory =
decayPltTy.getPointeeTypeCategory(
cast<TypedValue<mlir::acc::PointerLikeType>>(decayVal),
mlir::acc::getVarType(decayPtr.get()));
EXPECT_EQ(decayTypeCategory, mlir::acc::VariableTypeCategory::array);
// Create an element access.
mlir::Type i32Ty = cir::IntType::get(&context, 32, true);
mlir::Value index =
b.create<cir::ConstantOp>(loc, cir::IntAttr::get(i32Ty, 2));
OwningOpRef<cir::PtrStrideOp> accessPtr =
b.create<cir::PtrStrideOp>(loc, ptrToElemTy, decayVal, index);
mlir::Value accessVal = accessPtr.get();
// Verify that we still get the expected element type.
auto accessPltTy =
dyn_cast_if_present<mlir::acc::PointerLikeType>(accessVal.getType());
ASSERT_NE(accessPltTy, nullptr);
EXPECT_EQ(accessPltTy.getElementType(), ty);
// Verify that we still identify the type category as an array.
mlir::acc::VariableTypeCategory accessTypeCategory =
accessPltTy.getPointeeTypeCategory(
cast<TypedValue<mlir::acc::PointerLikeType>>(accessVal),
mlir::acc::getVarType(accessPtr.get()));
EXPECT_EQ(accessTypeCategory, mlir::acc::VariableTypeCategory::array);
}
// Structures and unions are accessed in the same way, so use a common test.
void testRecordType(mlir::Type ty1, mlir::Type ty2,
cir::RecordType::RecordKind kind) {
// Build the structure pointer type.
cir::RecordType structTy =
cir::RecordType::get(&context, getUniqueRecordName("S"), kind);
structTy.complete({ty1, ty2}, false, false);
mlir::Type ptrTy = cir::PointerType::get(structTy);
// Verify that the pointer points to the structure type.
auto pltTy = dyn_cast_if_present<mlir::acc::PointerLikeType>(ptrTy);
ASSERT_NE(pltTy, nullptr);
EXPECT_EQ(pltTy.getElementType(), structTy);
// Create an alloca for the array
OwningOpRef<cir::AllocaOp> varPtrOp = b.create<cir::AllocaOp>(
loc, ptrTy, structTy, "", getAlignOne(&context));
// Verify that the type category is composite.
mlir::Value val = varPtrOp.get();
mlir::acc::VariableTypeCategory typeCategory = pltTy.getPointeeTypeCategory(
cast<TypedValue<mlir::acc::PointerLikeType>>(val),
mlir::acc::getVarType(varPtrOp.get()));
EXPECT_EQ(typeCategory, mlir::acc::VariableTypeCategory::composite);
// Access the first element of the structure.
OwningOpRef<cir::GetMemberOp> access1 = b.create<cir::GetMemberOp>(
loc, cir::PointerType::get(ty1), val, b.getStringAttr("f1"), 0);
mlir::Value accessVal1 = access1.get();
// Verify that we get the expected element type.
auto access1PltTy =
dyn_cast_if_present<mlir::acc::PointerLikeType>(accessVal1.getType());
ASSERT_NE(access1PltTy, nullptr);
EXPECT_EQ(access1PltTy.getElementType(), ty1);
// Verify that the type category is still composite.
mlir::acc::VariableTypeCategory access1TypeCategory =
access1PltTy.getPointeeTypeCategory(
cast<TypedValue<mlir::acc::PointerLikeType>>(accessVal1),
mlir::acc::getVarType(access1.get()));
EXPECT_EQ(access1TypeCategory, mlir::acc::VariableTypeCategory::composite);
// Access the second element of the structure.
OwningOpRef<cir::GetMemberOp> access2 = b.create<cir::GetMemberOp>(
loc, cir::PointerType::get(ty2), val, b.getStringAttr("f2"), 1);
mlir::Value accessVal2 = access2.get();
// Verify that we get the expected element type.
auto access2PltTy =
dyn_cast_if_present<mlir::acc::PointerLikeType>(accessVal2.getType());
ASSERT_NE(access2PltTy, nullptr);
EXPECT_EQ(access2PltTy.getElementType(), ty2);
// Verify that the type category is still composite.
mlir::acc::VariableTypeCategory access2TypeCategory =
access2PltTy.getPointeeTypeCategory(
cast<TypedValue<mlir::acc::PointerLikeType>>(accessVal2),
mlir::acc::getVarType(access2.get()));
EXPECT_EQ(access2TypeCategory, mlir::acc::VariableTypeCategory::composite);
}
void testStructType(mlir::Type ty1, mlir::Type ty2) {
testRecordType(ty1, ty2, cir::RecordType::RecordKind::Struct);
}
void testUnionType(mlir::Type ty1, mlir::Type ty2) {
testRecordType(ty1, ty2, cir::RecordType::RecordKind::Union);
}
// This is testing a case like this:
//
// struct S {
// int *f1;
// int *f2;
// } *p;
// int *pMember = p->f2;
//
// That is, we are not testing a pointer to a member, we're testing a pointer
// that is loaded as a member value.
void testPointerToMemberType(
mlir::Type ty, mlir::acc::VariableTypeCategory expectedTypeCategory) {
// Construct a struct type with two members that are pointers to the input
// type.
mlir::Type ptrTy = cir::PointerType::get(ty);
cir::RecordType structTy =
cir::RecordType::get(&context, getUniqueRecordName("S"),
cir::RecordType::RecordKind::Struct);
structTy.complete({ptrTy, ptrTy}, false, false);
mlir::Type structPptrTy = cir::PointerType::get(structTy);
// Create an alloca for the struct.
OwningOpRef<cir::AllocaOp> varPtrOp = b.create<cir::AllocaOp>(
loc, structPptrTy, structTy, "S", getAlignOne(&context));
mlir::Value val = varPtrOp.get();
// Get a pointer to the second member.
OwningOpRef<cir::GetMemberOp> access = b.create<cir::GetMemberOp>(
loc, cir::PointerType::get(ptrTy), val, b.getStringAttr("f2"), 1);
mlir::Value accessVal = access.get();
// Load the value of the second member. This is the pointer we want to test.
OwningOpRef<cir::LoadOp> loadOp = b.create<cir::LoadOp>(loc, accessVal);
mlir::Value loadVal = loadOp.get();
// Verify that the type category is the expected type category.
auto pltTy = dyn_cast_if_present<mlir::acc::PointerLikeType>(ptrTy);
mlir::acc::VariableTypeCategory typeCategory = pltTy.getPointeeTypeCategory(
cast<TypedValue<mlir::acc::PointerLikeType>>(loadVal),
mlir::acc::getVarType(loadOp.get()));
EXPECT_EQ(typeCategory, expectedTypeCategory);
}
};
TEST_F(CIROpenACCPointerLikeTest, testPointerToInt) {
// Test various scalar types.
testScalarType(cir::IntType::get(&context, 8, true));
testScalarType(cir::IntType::get(&context, 8, false));
testScalarType(cir::IntType::get(&context, 16, true));
testScalarType(cir::IntType::get(&context, 16, false));
testScalarType(cir::IntType::get(&context, 32, true));
testScalarType(cir::IntType::get(&context, 32, false));
testScalarType(cir::IntType::get(&context, 64, true));
testScalarType(cir::IntType::get(&context, 64, false));
testScalarType(cir::IntType::get(&context, 128, true));
testScalarType(cir::IntType::get(&context, 128, false));
}
TEST_F(CIROpenACCPointerLikeTest, testPointerToBool) {
testScalarType(cir::BoolType::get(&context));
}
TEST_F(CIROpenACCPointerLikeTest, testPointerToFloat) {
testScalarType(cir::SingleType::get(&context));
testScalarType(cir::DoubleType::get(&context));
}
TEST_F(CIROpenACCPointerLikeTest, testPointerToPointer) {
mlir::Type i32Ty = cir::IntType::get(&context, 32, true);
mlir::Type ptrTy = cir::PointerType::get(i32Ty);
testScalarType(ptrTy);
}
TEST_F(CIROpenACCPointerLikeTest, testPointerToArray) {
// Test an array type.
mlir::Type i32Ty = cir::IntType::get(&context, 32, true);
testArrayType(i32Ty);
}
TEST_F(CIROpenACCPointerLikeTest, testPointerToStruct) {
// Test a struct type.
mlir::Type i16Ty = cir::IntType::get(&context, 16, true);
mlir::Type i32Ty = cir::IntType::get(&context, 32, true);
testStructType(i16Ty, i32Ty);
}
TEST_F(CIROpenACCPointerLikeTest, testPointerToUnion) {
// Test a union type.
mlir::Type i16Ty = cir::IntType::get(&context, 16, true);
mlir::Type i32Ty = cir::IntType::get(&context, 32, true);
testUnionType(i16Ty, i32Ty);
}
TEST_F(CIROpenACCPointerLikeTest, testPointerToFunction) {
mlir::Type i32Ty = cir::IntType::get(&context, 32, true);
mlir::Type funcTy =
cir::FuncType::get(SmallVector<mlir::Type, 2>{i32Ty, i32Ty}, i32Ty);
testNonScalarType(funcTy);
}
TEST_F(CIROpenACCPointerLikeTest, testPointerToVector) {
mlir::Type i32Ty = cir::IntType::get(&context, 32, true);
mlir::Type vecTy = cir::VectorType::get(i32Ty, 4);
testNonScalarType(vecTy);
}
TEST_F(CIROpenACCPointerLikeTest, testPointerToVoid) {
mlir::Type voidTy = cir::VoidType::get(&context);
testUncategorizedType(voidTy);
}
TEST_F(CIROpenACCPointerLikeTest, testPointerToIntMember) {
mlir::Type i32Ty = cir::IntType::get(&context, 32, true);
testPointerToMemberType(i32Ty, mlir::acc::VariableTypeCategory::scalar);
}
TEST_F(CIROpenACCPointerLikeTest, testPointerToArrayMember) {
mlir::Type i32Ty = cir::IntType::get(&context, 32, true);
mlir::Type arrTy = cir::ArrayType::get(i32Ty, 10);
testPointerToMemberType(arrTy, mlir::acc::VariableTypeCategory::array);
}
TEST_F(CIROpenACCPointerLikeTest, testPointerToStructMember) {
mlir::Type i32Ty = cir::IntType::get(&context, 32, true);
cir::RecordType structTy = cir::RecordType::get(
&context, getUniqueRecordName("S"), cir::RecordType::RecordKind::Struct);
structTy.complete({i32Ty, i32Ty}, false, false);
testPointerToMemberType(structTy, mlir::acc::VariableTypeCategory::composite);
}
|