@@ -36,6 +36,11 @@ using OpenACCIRBuilder = llvm::OpenMPIRBuilder;
3636static constexpr uint64_t createFlag = 0 ;
3737// / 1 = to/copyin
3838static constexpr uint64_t copyinFlag = 1 ;
39+ // / 2 = from/copyout
40+ static constexpr uint64_t copyoutFlag = 2 ;
41+ // / 8 = delete
42+ static constexpr uint64_t deleteFlag = 8 ;
43+
3944// / Default value for the device id
4045static constexpr int64_t defaultDevice = -1 ;
4146
@@ -57,10 +62,10 @@ static llvm::Constant *createSourceLocStrFromLocation(Location loc,
5762}
5863
5964// / Create the location struct from the operation location information.
60- static llvm::Value *createSourceLocationInfo (acc::EnterDataOp &op ,
61- OpenACCIRBuilder &builder ) {
62- auto loc = op. getLoc ();
63- auto funcOp = op. getOperation () ->getParentOfType <LLVM::LLVMFuncOp>();
65+ static llvm::Value *createSourceLocationInfo (OpenACCIRBuilder &builder ,
66+ Operation *op ) {
67+ auto loc = op-> getLoc ();
68+ auto funcOp = op->getParentOfType <LLVM::LLVMFuncOp>();
6469 StringRef funcName = funcOp ? funcOp.getName () : " unknown" ;
6570 llvm::Constant *locStr =
6671 createSourceLocStrFromLocation (loc, builder, funcName);
@@ -81,10 +86,16 @@ static llvm::Constant *createMappingInformation(Location loc,
8186
8287// / Return the runtime function used to lower the given operation.
8388static llvm::Function *getAssociatedFunction (OpenACCIRBuilder &builder,
84- Operation &op) {
85- if (isa<acc::EnterDataOp>(op))
86- return builder.getOrCreateRuntimeFunctionPtr (
87- llvm::omp::OMPRTL___tgt_target_data_begin_mapper);
89+ Operation *op) {
90+ return llvm::TypeSwitch<Operation *, llvm::Function *>(op)
91+ .Case ([&](acc::EnterDataOp) {
92+ return builder.getOrCreateRuntimeFunctionPtr (
93+ llvm::omp::OMPRTL___tgt_target_data_begin_mapper);
94+ })
95+ .Case ([&](acc::ExitDataOp) {
96+ return builder.getOrCreateRuntimeFunctionPtr (
97+ llvm::omp::OMPRTL___tgt_target_data_end_mapper);
98+ });
8899 llvm_unreachable (" Unknown OpenACC operation" );
89100}
90101
@@ -105,7 +116,7 @@ static llvm::Value *getSizeInBytes(llvm::IRBuilderBase &builder,
105116// / to populate the future functions arguments.
106117static LogicalResult
107118processOperands (llvm::IRBuilderBase &builder,
108- LLVM::ModuleTranslation &moduleTranslation, Operation & op,
119+ LLVM::ModuleTranslation &moduleTranslation, Operation * op,
109120 ValueRange operands, unsigned totalNbOperand,
110121 uint64_t operandFlag, SmallVector<uint64_t > &flags,
111122 SmallVector<llvm::Constant *> &names, unsigned &index,
@@ -137,7 +148,7 @@ processOperands(llvm::IRBuilderBase &builder,
137148 dataPtr = dataValue;
138149 dataSize = getSizeInBytes (builder, dataValue);
139150 } else {
140- return op. emitOpError ()
151+ return op-> emitOpError ()
141152 << " Data operand must be legalized before translation."
142153 << " Unsupported type: " << data.getType ();
143154 }
@@ -171,28 +182,81 @@ processOperands(llvm::IRBuilderBase &builder,
171182 return success ();
172183}
173184
185+ // / Process data operands from acc::EnterDataOp
186+ static LogicalResult
187+ processDataOperands (llvm::IRBuilderBase &builder,
188+ LLVM::ModuleTranslation &moduleTranslation,
189+ acc::EnterDataOp op, SmallVector<uint64_t > &flags,
190+ SmallVector<llvm::Constant *> &names, unsigned &index,
191+ llvm::AllocaInst *argsBase, llvm::AllocaInst *args,
192+ llvm::AllocaInst *argSizes) {
193+ // TODO add `create_zero` and `attach` operands
194+
195+ // Create operands are handled as `alloc` call.
196+ if (failed (processOperands (builder, moduleTranslation, op,
197+ op.createOperands (), op.getNumDataOperands (),
198+ createFlag, flags, names, index, argsBase, args,
199+ argSizes)))
200+ return failure ();
201+
202+ // Copyin operands are handled as `to` call.
203+ if (failed (processOperands (builder, moduleTranslation, op,
204+ op.copyinOperands (), op.getNumDataOperands (),
205+ copyinFlag, flags, names, index, argsBase, args,
206+ argSizes)))
207+ return failure ();
208+
209+ return success ();
210+ }
211+
212+ // / Process data operands from acc::ExitDataOp
213+ static LogicalResult
214+ processDataOperands (llvm::IRBuilderBase &builder,
215+ LLVM::ModuleTranslation &moduleTranslation,
216+ acc::ExitDataOp op, SmallVector<uint64_t > &flags,
217+ SmallVector<llvm::Constant *> &names, unsigned &index,
218+ llvm::AllocaInst *argsBase, llvm::AllocaInst *args,
219+ llvm::AllocaInst *argSizes) {
220+ // TODO add `detach` operands
221+
222+ // Delete operands are handled as `delete` call.
223+ if (failed (processOperands (builder, moduleTranslation, op,
224+ op.deleteOperands (), op.getNumDataOperands (),
225+ deleteFlag, flags, names, index, argsBase, args,
226+ argSizes)))
227+ return failure ();
228+
229+ // Copyout operands are handled as `from` call.
230+ if (failed (processOperands (builder, moduleTranslation, op,
231+ op.copyoutOperands (), op.getNumDataOperands (),
232+ copyoutFlag, flags, names, index, argsBase, args,
233+ argSizes)))
234+ return failure ();
235+
236+ return success ();
237+ }
238+
174239// ===----------------------------------------------------------------------===//
175240// Conversion functions
176241// ===----------------------------------------------------------------------===//
177242
178- // / Converts an OpenACC enter_data operartion into LLVM IR.
243+ // / Converts an OpenACC standalone data operation into LLVM IR.
244+ template <typename OpTy>
179245static LogicalResult
180- convertEnterDataOp (Operation &op, llvm::IRBuilderBase &builder,
181- LLVM::ModuleTranslation &moduleTranslation) {
182- auto enterDataOp = cast<acc::EnterDataOp>(op);
183- auto enclosingFuncOp = op.getParentOfType <LLVM::LLVMFuncOp>();
246+ convertStandaloneDataOp (OpTy &op, llvm::IRBuilderBase &builder,
247+ LLVM::ModuleTranslation &moduleTranslation) {
248+ auto enclosingFuncOp =
249+ op.getOperation ()-> template getParentOfType <LLVM::LLVMFuncOp>();
184250 llvm::Function *enclosingFunction =
185251 moduleTranslation.lookupFunction (enclosingFuncOp.getName ());
186252
187253 OpenACCIRBuilder *accBuilder = moduleTranslation.getOpenMPBuilder ();
188254
189- auto *srcLocInfo = createSourceLocationInfo (enterDataOp, *accBuilder);
255+ auto *srcLocInfo = createSourceLocationInfo (*accBuilder, op );
190256 auto *mapperFunc = getAssociatedFunction (*accBuilder, op);
191257
192258 // Number of arguments in the enter_data operation.
193- // TODO include create_zero and attach operands.
194- unsigned totalNbOperand =
195- enterDataOp.createOperands ().size () + enterDataOp.copyinOperands ().size ();
259+ unsigned totalNbOperand = op.getNumDataOperands ();
196260
197261 // TODO could be moved to OpenXXIRBuilder?
198262 llvm::LLVMContext &ctx = builder.getContext ();
@@ -214,18 +278,8 @@ convertEnterDataOp(Operation &op, llvm::IRBuilderBase &builder,
214278 SmallVector<llvm::Constant *> names;
215279 unsigned index = 0 ;
216280
217- // Create operands are handled as `alloc` call.
218- if (failed (processOperands (builder, moduleTranslation, op,
219- enterDataOp.createOperands (), totalNbOperand,
220- createFlag, flags, names, index, argsBase, args,
221- argSizes)))
222- return failure ();
223-
224- // Copyin operands are handled as `to` call.
225- if (failed (processOperands (builder, moduleTranslation, op,
226- enterDataOp.copyinOperands (), totalNbOperand,
227- copyinFlag, flags, names, index, argsBase, args,
228- argSizes)))
281+ if (failed (processDataOperands (builder, moduleTranslation, op, flags, names,
282+ index, argsBase, args, argSizes)))
229283 return failure ();
230284
231285 llvm::GlobalVariable *maptypes =
@@ -282,8 +336,13 @@ LogicalResult OpenACCDialectLLVMIRTranslationInterface::convertOperation(
282336 LLVM::ModuleTranslation &moduleTranslation) const {
283337
284338 return llvm::TypeSwitch<Operation *, LogicalResult>(op)
285- .Case ([&](acc::EnterDataOp) {
286- return convertEnterDataOp (*op, builder, moduleTranslation);
339+ .Case ([&](acc::EnterDataOp enterDataOp) {
340+ return convertStandaloneDataOp<acc::EnterDataOp>(enterDataOp, builder,
341+ moduleTranslation);
342+ })
343+ .Case ([&](acc::ExitDataOp exitDataOp) {
344+ return convertStandaloneDataOp<acc::ExitDataOp>(exitDataOp, builder,
345+ moduleTranslation);
287346 })
288347 .Default ([&](Operation *op) {
289348 return op->emitError (" unsupported OpenACC operation: " )
0 commit comments