1//===- TestPointerLikeTypeInterface.cpp - Test PointerLikeType interface -===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains test passes for testing the OpenACC PointerLikeType
10// interface methods.
11//
12//===----------------------------------------------------------------------===//
13
14#include "mlir/Dialect/Arith/IR/Arith.h"
15#include "mlir/Dialect/Func/IR/FuncOps.h"
16#include "mlir/Dialect/MemRef/IR/MemRefDialect.h"
17#include "mlir/Dialect/OpenACC/OpenACC.h"
18#include "mlir/IR/Builders.h"
19#include "mlir/Pass/Pass.h"
20#include "llvm/Support/CommandLine.h"
21
22using namespace mlir;
23using namespace mlir::acc;
24
25namespace {
26
27struct OperationTracker : public OpBuilder::Listener {
28 SmallVector<Operation *> insertedOps;
29
30 void notifyOperationInserted(Operation *op,
31 OpBuilder::InsertPoint previous) override {
32 insertedOps.push_back(Elt: op);
33 }
34};
35
36struct TestPointerLikeTypeInterfacePass
37 : public PassWrapper<TestPointerLikeTypeInterfacePass,
38 OperationPass<func::FuncOp>> {
39 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestPointerLikeTypeInterfacePass)
40
41 TestPointerLikeTypeInterfacePass() = default;
42 TestPointerLikeTypeInterfacePass(const TestPointerLikeTypeInterfacePass &pass)
43 : PassWrapper(pass) {
44 testMode = pass.testMode;
45 }
46
47 Pass::Option<std::string> testMode{
48 *this, "test-mode",
49 llvm::cl::desc(
50 "Test mode: walk, alloc, copy, free, load, store, or cast"),
51 llvm::cl::init(Val: "walk")};
52
53 StringRef getArgument() const override {
54 return "test-acc-pointer-like-interface";
55 }
56
57 StringRef getDescription() const override {
58 return "Test OpenACC PointerLikeType interface methods on any implementing "
59 "type";
60 }
61
62 void runOnOperation() override;
63
64 void getDependentDialects(DialectRegistry &registry) const override {
65 registry.insert<acc::OpenACCDialect>();
66 registry.insert<arith::ArithDialect>();
67 registry.insert<memref::MemRefDialect>();
68 }
69
70private:
71 void walkAndPrint();
72 void testGenAllocate(Operation *op, Value result, PointerLikeType pointerType,
73 OpBuilder &builder);
74 void testGenFree(Operation *op, Value result, PointerLikeType pointerType,
75 OpBuilder &builder);
76 void testGenCopy(Operation *srcOp, Operation *destOp, Value srcResult,
77 Value destResult, PointerLikeType pointerType,
78 OpBuilder &builder);
79 void testGenLoad(Operation *op, Value result, PointerLikeType pointerType,
80 OpBuilder &builder);
81 void testGenStore(Operation *op, Value result, PointerLikeType pointerType,
82 OpBuilder &builder, Value providedValue = {});
83 void testGenCast(Operation *op, Value value, Type resultType,
84 OpBuilder &builder);
85
86 struct PointerCandidate {
87 Operation *op;
88 Value result;
89 PointerLikeType pointerType;
90 };
91};
92
93void TestPointerLikeTypeInterfacePass::runOnOperation() {
94 if (testMode == "walk") {
95 walkAndPrint();
96 return;
97 }
98
99 auto func = getOperation();
100 OpBuilder builder(&getContext());
101
102 if (testMode == "cast") {
103 func.walk(callback: [&](Operation *op) {
104 if (!op->hasDiscardableAttr(name: "test.cast"))
105 return;
106 auto destAttr =
107 dyn_cast_or_null<TypeAttr>(Val: op->getDiscardableAttr(name: "cast_dest"));
108 if (!destAttr || op->getNumResults() == 0)
109 return;
110 testGenCast(op, value: op->getResult(idx: 0), resultType: destAttr.getValue(), builder);
111 });
112 return;
113 }
114
115 if (testMode == "alloc" || testMode == "free" || testMode == "load" ||
116 testMode == "store") {
117 // Collect all candidates first
118 SmallVector<PointerCandidate> candidates;
119 // For store mode, also look for a test value to use
120 Value testValue;
121 func.walk(callback: [&](Operation *op) {
122 if (op->hasDiscardableAttr(name: "test.ptr")) {
123 for (auto result : op->getResults()) {
124 if (isa<PointerLikeType>(Val: result.getType())) {
125 candidates.push_back(
126 Elt: {.op: op, .result: result, .pointerType: cast<PointerLikeType>(Val: result.getType())});
127 break; // Only take the first PointerLikeType result
128 }
129 }
130 }
131 // Collect value marked with test.value for store tests
132 if (testMode == "store" && op->hasDiscardableAttr(name: "test.value")) {
133 if (op->getNumResults() > 0)
134 testValue = op->getResult(idx: 0);
135 }
136 });
137
138 // Now test all candidates
139 for (const auto &candidate : candidates) {
140 if (testMode == "alloc")
141 testGenAllocate(op: candidate.op, result: candidate.result, pointerType: candidate.pointerType,
142 builder);
143 else if (testMode == "free")
144 testGenFree(op: candidate.op, result: candidate.result, pointerType: candidate.pointerType,
145 builder);
146 else if (testMode == "load")
147 testGenLoad(op: candidate.op, result: candidate.result, pointerType: candidate.pointerType,
148 builder);
149 else if (testMode == "store")
150 testGenStore(op: candidate.op, result: candidate.result, pointerType: candidate.pointerType,
151 builder, providedValue: testValue);
152 }
153 } else if (testMode == "copy") {
154 // Collect all source and destination candidates
155 SmallVector<PointerCandidate> sources, destinations;
156
157 func.walk(callback: [&](Operation *op) {
158 if (op->hasDiscardableAttr(name: "test.src_ptr")) {
159 for (auto result : op->getResults()) {
160 if (isa<PointerLikeType>(Val: result.getType())) {
161 sources.push_back(
162 Elt: {.op: op, .result: result, .pointerType: cast<PointerLikeType>(Val: result.getType())});
163 break;
164 }
165 }
166 }
167 if (op->hasDiscardableAttr(name: "test.dest_ptr")) {
168 for (auto result : op->getResults()) {
169 if (isa<PointerLikeType>(Val: result.getType())) {
170 destinations.push_back(
171 Elt: {.op: op, .result: result, .pointerType: cast<PointerLikeType>(Val: result.getType())});
172 break;
173 }
174 }
175 }
176 });
177
178 // Try copying from each source to each destination
179 for (const auto &src : sources)
180 for (const auto &dest : destinations)
181 testGenCopy(srcOp: src.op, destOp: dest.op, srcResult: src.result, destResult: dest.result, pointerType: src.pointerType,
182 builder);
183 }
184}
185
186void TestPointerLikeTypeInterfacePass::walkAndPrint() {
187 auto func = getOperation();
188
189 func.walk(callback: [&](Operation *op) {
190 // Look for operations marked with "test.ptr", "test.src_ptr", or
191 // "test.dest_ptr"
192 if (op->hasDiscardableAttr(name: "test.ptr") ||
193 op->hasDiscardableAttr(name: "test.src_ptr") ||
194 op->hasDiscardableAttr(name: "test.dest_ptr")) {
195 llvm::errs() << "Operation: ";
196 op->print(os&: llvm::errs());
197 llvm::errs() << "\n";
198
199 // Check each result to see if it's a PointerLikeType
200 for (auto result : op->getResults()) {
201 if (isa<PointerLikeType>(Val: result.getType())) {
202 llvm::errs() << " Result " << result.getResultNumber()
203 << " is PointerLikeType: ";
204 result.getType().print(os&: llvm::errs());
205 llvm::errs() << "\n";
206 } else {
207 llvm::errs() << " Result " << result.getResultNumber()
208 << " is NOT PointerLikeType: ";
209 result.getType().print(os&: llvm::errs());
210 llvm::errs() << "\n";
211 }
212 }
213
214 if (op->getNumResults() == 0)
215 llvm::errs() << " Operation has no results\n";
216
217 llvm::errs() << "\n";
218 }
219 });
220}
221
222void TestPointerLikeTypeInterfacePass::testGenAllocate(
223 Operation *op, Value result, PointerLikeType pointerType,
224 OpBuilder &builder) {
225 Location loc = op->getLoc();
226
227 // Create a new builder with the listener and set insertion point
228 OperationTracker tracker;
229 OpBuilder newBuilder(op->getContext());
230 newBuilder.setListener(&tracker);
231 newBuilder.setInsertionPointAfter(op);
232
233 // Call the genAllocate API
234 bool needsFree = false;
235 Value allocRes = pointerType.genAllocate(builder&: newBuilder, loc, varName: "test_alloc",
236 varType: result.getType(), originalVar: result, needsFree);
237
238 if (allocRes) {
239 llvm::errs() << "Successfully generated alloc for operation: ";
240 op->print(os&: llvm::errs());
241 llvm::errs() << "\n";
242 llvm::errs() << "\tneeds free: " << (needsFree ? "true" : "false") << "\n";
243
244 // Print all operations that were inserted
245 for (Operation *insertedOp : tracker.insertedOps) {
246 llvm::errs() << "\tGenerated: ";
247 insertedOp->print(os&: llvm::errs());
248 llvm::errs() << "\n";
249 }
250 } else {
251 llvm::errs() << "Failed to generate alloc for operation: ";
252 op->print(os&: llvm::errs());
253 llvm::errs() << "\n";
254 }
255}
256
257void TestPointerLikeTypeInterfacePass::testGenFree(Operation *op, Value result,
258 PointerLikeType pointerType,
259 OpBuilder &builder) {
260 Location loc = op->getLoc();
261
262 // Create a new builder with the listener and set insertion point
263 OperationTracker tracker;
264 OpBuilder newBuilder(op->getContext());
265 newBuilder.setListener(&tracker);
266 newBuilder.setInsertionPointAfter(op);
267
268 // Call the genFree API
269 auto typedResult = cast<TypedValue<PointerLikeType>>(Val&: result);
270 bool success = pointerType.genFree(builder&: newBuilder, loc, varToFree: typedResult, allocRes: result,
271 varType: result.getType());
272
273 if (success) {
274 llvm::errs() << "Successfully generated free for operation: ";
275 op->print(os&: llvm::errs());
276 llvm::errs() << "\n";
277
278 // Print all operations that were inserted
279 for (Operation *insertedOp : tracker.insertedOps) {
280 llvm::errs() << "\tGenerated: ";
281 insertedOp->print(os&: llvm::errs());
282 llvm::errs() << "\n";
283 }
284 } else {
285 llvm::errs() << "Failed to generate free for operation: ";
286 op->print(os&: llvm::errs());
287 llvm::errs() << "\n";
288 }
289}
290
291void TestPointerLikeTypeInterfacePass::testGenCopy(
292 Operation *srcOp, Operation *destOp, Value srcResult, Value destResult,
293 PointerLikeType pointerType, OpBuilder &builder) {
294 Location loc = destOp->getLoc();
295
296 // Create a new builder with the listener and set insertion point
297 OperationTracker tracker;
298 OpBuilder newBuilder(destOp->getContext());
299 newBuilder.setListener(&tracker);
300 newBuilder.setInsertionPointAfter(destOp);
301
302 // Call the genCopy API with the provided source and destination
303 auto typedSrc = cast<TypedValue<PointerLikeType>>(Val&: srcResult);
304 auto typedDest = cast<TypedValue<PointerLikeType>>(Val&: destResult);
305 bool success = pointerType.genCopy(builder&: newBuilder, loc, destination: typedDest, source: typedSrc,
306 varType: srcResult.getType());
307
308 if (success) {
309 llvm::errs() << "Successfully generated copy from source: ";
310 srcOp->print(os&: llvm::errs());
311 llvm::errs() << " to destination: ";
312 destOp->print(os&: llvm::errs());
313 llvm::errs() << "\n";
314
315 // Print all operations that were inserted
316 for (Operation *insertedOp : tracker.insertedOps) {
317 llvm::errs() << "\tGenerated: ";
318 insertedOp->print(os&: llvm::errs());
319 llvm::errs() << "\n";
320 }
321 } else {
322 llvm::errs() << "Failed to generate copy from source: ";
323 srcOp->print(os&: llvm::errs());
324 llvm::errs() << " to destination: ";
325 destOp->print(os&: llvm::errs());
326 llvm::errs() << "\n";
327 }
328}
329
330void TestPointerLikeTypeInterfacePass::testGenLoad(Operation *op, Value result,
331 PointerLikeType pointerType,
332 OpBuilder &builder) {
333 Location loc = op->getLoc();
334
335 // Create a new builder with the listener and set insertion point
336 OperationTracker tracker;
337 OpBuilder newBuilder(op->getContext());
338 newBuilder.setListener(&tracker);
339 newBuilder.setInsertionPointAfter(op);
340
341 // Call the genLoad API
342 auto typedResult = cast<TypedValue<PointerLikeType>>(Val&: result);
343 Value loadRes = pointerType.genLoad(builder&: newBuilder, loc, srcPtr: typedResult, valueType: Type());
344
345 if (loadRes) {
346 llvm::errs() << "Successfully generated load for operation: ";
347 op->print(os&: llvm::errs());
348 llvm::errs() << "\n";
349 llvm::errs() << "\tLoaded value type: ";
350 loadRes.getType().print(os&: llvm::errs());
351 llvm::errs() << "\n";
352
353 // Print all operations that were inserted
354 for (Operation *insertedOp : tracker.insertedOps) {
355 llvm::errs() << "\tGenerated: ";
356 insertedOp->print(os&: llvm::errs());
357 llvm::errs() << "\n";
358 }
359 } else {
360 llvm::errs() << "Failed to generate load for operation: ";
361 op->print(os&: llvm::errs());
362 llvm::errs() << "\n";
363 }
364}
365
366void TestPointerLikeTypeInterfacePass::testGenStore(Operation *op, Value result,
367 PointerLikeType pointerType,
368 OpBuilder &builder,
369 Value providedValue) {
370 Location loc = op->getLoc();
371
372 // Create a new builder with the listener and set insertion point
373 OperationTracker tracker;
374 OpBuilder newBuilder(op->getContext());
375 newBuilder.setListener(&tracker);
376 newBuilder.setInsertionPointAfter(op);
377
378 // Use provided value if available, otherwise create a constant
379 Value valueToStore = providedValue;
380 if (!valueToStore) {
381 // Create a test value to store - use a constant matching the element type
382 Type elementType = pointerType.getElementType();
383 if (!elementType) {
384 llvm::errs() << "Failed to generate store for operation: ";
385 op->print(os&: llvm::errs());
386 llvm::errs() << "\n";
387 return;
388 }
389
390 if (elementType.isIntOrIndex()) {
391 auto attr = newBuilder.getIntegerAttr(type: elementType, value: 42);
392 valueToStore =
393 arith::ConstantOp::create(builder&: newBuilder, location: loc, result: elementType, value: attr);
394 } else if (auto floatType = dyn_cast<FloatType>(Val&: elementType)) {
395 auto attr = newBuilder.getFloatAttr(type: floatType, value: 42.0);
396 valueToStore =
397 arith::ConstantOp::create(builder&: newBuilder, location: loc, result: floatType, value: attr);
398 } else {
399 llvm::errs() << "Failed to generate store for operation: ";
400 op->print(os&: llvm::errs());
401 llvm::errs() << "\n";
402 return;
403 }
404 }
405
406 // Call the genStore API
407 auto typedResult = cast<TypedValue<PointerLikeType>>(Val&: result);
408 bool success =
409 pointerType.genStore(builder&: newBuilder, loc, valueToStore, destPtr: typedResult);
410
411 if (success) {
412 llvm::errs() << "Successfully generated store for operation: ";
413 op->print(os&: llvm::errs());
414 llvm::errs() << "\n";
415
416 // Print all operations that were inserted
417 for (Operation *insertedOp : tracker.insertedOps) {
418 llvm::errs() << "\tGenerated: ";
419 insertedOp->print(os&: llvm::errs());
420 llvm::errs() << "\n";
421 }
422 } else {
423 llvm::errs() << "Failed to generate store for operation: ";
424 op->print(os&: llvm::errs());
425 llvm::errs() << "\n";
426 }
427}
428
429void TestPointerLikeTypeInterfacePass::testGenCast(Operation *op, Value value,
430 Type resultType,
431 OpBuilder &builder) {
432 Location loc = op->getLoc();
433
434 OperationTracker tracker;
435 OpBuilder newBuilder(op->getContext());
436 newBuilder.setListener(&tracker);
437 newBuilder.setInsertionPointAfter(op);
438
439 PointerLikeType dispatchTy;
440 if (isa<PointerLikeType>(Val: value.getType()))
441 dispatchTy = cast<PointerLikeType>(Val: value.getType());
442 else if (isa<PointerLikeType>(Val: resultType))
443 dispatchTy = cast<PointerLikeType>(Val&: resultType);
444 else {
445 llvm::errs() << "Failed genCast: neither value nor result type is "
446 "PointerLikeType for operation: ";
447 op->print(os&: llvm::errs());
448 llvm::errs() << "\n";
449 return;
450 }
451
452 Value castRes = dispatchTy.genCast(builder&: newBuilder, loc, value, resultType);
453
454 if (castRes) {
455 llvm::errs() << "Successfully generated cast for operation: ";
456 op->print(os&: llvm::errs());
457 llvm::errs() << "\n";
458 llvm::errs() << "\tCast result type: ";
459 castRes.getType().print(os&: llvm::errs());
460 llvm::errs() << "\n";
461
462 for (Operation *insertedOp : tracker.insertedOps) {
463 llvm::errs() << "\tGenerated: ";
464 insertedOp->print(os&: llvm::errs());
465 llvm::errs() << "\n";
466 }
467 } else {
468 llvm::errs() << "Failed to generate cast for operation: ";
469 op->print(os&: llvm::errs());
470 llvm::errs() << "\n";
471 }
472}
473
474} // namespace
475
476//===----------------------------------------------------------------------===//
477// Pass Registration
478//===----------------------------------------------------------------------===//
479
480namespace mlir {
481namespace test {
482void registerTestPointerLikeTypeInterfacePass() {
483 PassRegistration<TestPointerLikeTypeInterfacePass>();
484}
485} // namespace test
486} // namespace mlir
487