1 //===- ConvertOpenACCToLLVM.h - OpenACC conversion pass entrypoint --------===//
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 #ifndef MLIR_CONVERSION_OPENACCTOLLVM_CONVERTOPENACCTOLLVM_H
9 #define MLIR_CONVERSION_OPENACCTOLLVM_CONVERTOPENACCTOLLVM_H
10 
11 #include "mlir/Conversion/LLVMCommon/StructBuilder.h"
12 #include <memory>
13 
14 namespace mlir {
15 class LLVMTypeConverter;
16 class ModuleOp;
17 template <typename T>
18 class OperationPass;
19 class RewritePatternSet;
20 
21 static constexpr unsigned kPtrBasePosInDataDescriptor = 0;
22 static constexpr unsigned kPtrPosInDataDescriptor = 1;
23 static constexpr unsigned kSizePosInDataDescriptor = 2;
24 
25 /// Helper class to produce LLVM dialect operations inserting
26 /// elements to a Data descriptor. Wraps a Value pointing to the descriptor.
27 /// The Value may be null, in which case none of the operations are valid.
28 ///
29 /// The data descriptor holds information needed to perform data operations
30 /// and movments with the runtime.
31 /// `BasePointer`: base of the pointer being mapped.
32 /// `Pointer`: actual pointer of the data being mapped.
33 /// `Size`: size of the data being mapped.
34 ///
35 /// Example:
36 ///
37 /// ```c
38 /// struct S {
39 ///   int x;
40 ///   int y;
41 /// };
42 /// ```
43 ///
44 /// Mapping `s.y` will result if the following information in the data
45 /// descriptor:
46 /// - `BasePointer`: address of `s`
47 /// - `Pointer`: address of `s.y`
48 /// - `Size`: size of `s.y`
49 ///
50 /// For a scalar variable BasePointer and Pointer will be the same.
51 class DataDescriptor : public StructBuilder {
52 public:
53   /// Construct a helper for the given descriptor value.
54   explicit DataDescriptor(Value descriptor);
55   /// Builds IR creating an `undef` value of the descriptor type.
56   static DataDescriptor undef(OpBuilder &builder, Location loc, Type basePtrTy,
57                               Type ptrTy);
58 
59   static bool isValid(Value descriptor);
60 
61   void setPointer(OpBuilder &builder, Location loc, Value ptr);
62   void setBasePointer(OpBuilder &builder, Location loc, Value basePtr);
63   void setSize(OpBuilder &builder, Location loc, Value size);
64 };
65 
66 /// Collect the patterns to convert from the OpenACC dialect LLVMIR dialect.
67 void populateOpenACCToLLVMConversionPatterns(LLVMTypeConverter &converter,
68                                              RewritePatternSet &patterns);
69 
70 /// Create a pass to convert the OpenACC dialect into the LLVMIR dialect.
71 std::unique_ptr<OperationPass<ModuleOp>> createConvertOpenACCToLLVMPass();
72 
73 } // namespace mlir
74 
75 #endif // MLIR_CONVERSION_OPENACCTOLLVM_CONVERTOPENACCTOLLVM_H
76