MLIR: include/mlir/Target/LLVMIR/ModuleTranslation.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14 #ifndef MLIR_TARGET_LLVMIR_MODULETRANSLATION_H

15 #define MLIR_TARGET_LLVMIR_MODULETRANSLATION_H

16

24

25 #include "llvm/ADT/SetVector.h"

26 #include "llvm/IR/FPEnv.h"

27

28 namespace llvm {

29 class BasicBlock;

31 class IRBuilderBase;

32 class OpenMPIRBuilder;

34 }

35

36 namespace mlir {

37 class Attribute;

39 class Location;

40

41 namespace LLVM {

42

43 namespace detail {

44 class DebugTranslation;

45 class LoopAnnotationTranslation;

46 }

47

48 class AliasScopeAttr;

49 class AliasScopeDomainAttr;

50 class DINodeAttr;

51 class LLVMFuncOp;

52 class ComdatSelectorOp;

53

54

55

56

57

58

60 friend std::unique_ptrllvm::Module

62 bool);

63

64 public:

65

66 void mapFunction(StringRef name, llvm::Function *func) {

67 auto result = functionMapping.try_emplace(name, func);

68 (void)result;

69 assert(result.second &&

70 "attempting to map a function that is already mapped");

71 }

72

73

75 return functionMapping.lookup(name);

76 }

77

78

80

81

82

84 llvm::Value *&llvm = valueMapping[value];

85 assert(llvm == nullptr &&

86 "attempting to map a value that is already mapped");

88 }

89

90

92 return valueMapping.lookup(value);

93 }

94

95

97

98

100 auto result = blockMapping.try_emplace(mlir, llvm);

101 (void)result;

102 assert(result.second && "attempting to map a block that is already mapped");

103 }

104

105

107 return blockMapping.lookup(block);

108 }

109

110

111

113 auto result = branchMapping.try_emplace(mlir, llvm);

114 (void)result;

115 assert(result.second &&

116 "attempting to map a branch that is already mapped");

117 }

118

119

120

122 return branchMapping.lookup(op);

123 }

124

125

126

128 auto result = callMapping.try_emplace(mlir, llvm);

129 (void)result;

130 assert(result.second && "attempting to map a call that is already mapped");

131 }

132

133

134

136 return callMapping.lookup(op);

137 }

138

139

140

142 auto result = unresolvedBlockAddressMapping.try_emplace(op, cst);

143 (void)result;

144 assert(result.second &&

145 "attempting to map a blockaddress operation that is already mapped");

146 }

147

148

149 void mapBlockAddress(BlockAddressAttr attr, llvm::BasicBlock *block) {

150 auto result = blockAddressToLLVMMapping.try_emplace(attr, block);

151 (void)result;

152 assert(result.second &&

153 "attempting to map a blockaddress attribute that is already mapped");

154 }

155

156

158 return blockAddressToLLVMMapping.lookup(attr);

159 }

160

161

162

164

165

166

168

169

170

171

172 llvm::MDNode *

174

175

177 llvm::Instruction *inst);

178

179

181 llvm::Instruction *inst);

182

183

184 void setTBAAMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst);

185

186

187

189 llvm::Instruction *inst);

190

191

193

194

195

197

198

199

200

201

203

204

206

207

209

210

211 llvm::LLVMContext &getLLVMContext() const { return llvmModule->getContext(); }

212

213

214

216 return globalsMapping.lookup(op);

217 }

218

219

220

222 return aliasesMapping.lookup(op);

223 }

224

225

226

228

229

231

232

234

235

237

238

239 llvm::DIGlobalVariableExpression *

241

242

244

245

247

248

249 llvm::fp::ExceptionBehavior

251

252

253

254

255

256

257

258

260 llvm::IRBuilderBase &builder) {

261 return convertBlockImpl(bb, ignoreArguments, builder,

262 false);

263 }

264

265

266

268 DictionaryAttr paramAttrs);

269

270

271

273

274

276 public:

279

280 protected:

282

283 private:

285 virtual void anchor();

286 };

287

288

289

290

291

292

293

294

295 template

297 public:

299 };

300

301

302

303

304

305

306 template <typename T, typename... Args>

308 static_assert(

309 std::is_base_of<StackFrame, T>::value,

310 "can only push instances of StackFrame on ModuleTranslation stack");

311 stack.push_back(std::make_unique(std::forward(args)...));

312 }

313

314

316

317

318

319 template

321 static_assert(std::is_base_of<StackFrame, T>::value,

322 "expected T derived from StackFrame");

323 if (!callback)

325 for (std::unique_ptr &frame : llvm::reverse(stack)) {

326 if (T *ptr = dyn_cast_or_null(frame.get())) {

329 return result;

330 }

331 }

333 }

334

335

336 template

338 template <typename... Args>

340 : moduleTranslation(m) {

341 moduleTranslation.stackPush(std::forward(args)...);

342 }

344

345 private:

347 };

348

350

351 private:

353 std::unique_ptrllvm::Module llvmModule);

355

356

357 LogicalResult convertOperation(Operation &op, llvm::IRBuilderBase &builder,

358 bool recordInsertions = false);

359 LogicalResult convertFunctionSignatures();

360 LogicalResult convertFunctions();

361 LogicalResult convertComdats();

362

363 LogicalResult convertUnresolvedBlockAddress();

364

365

366

367

368

369

370 LogicalResult convertGlobalsAndAliases();

371 LogicalResult convertOneFunction(LLVMFuncOp func);

372 LogicalResult convertBlockImpl(Block &bb, bool ignoreArguments,

373 llvm::IRBuilderBase &builder,

374 bool recordInsertions);

375

376

377

378 llvm::MDNode *getTBAANode(TBAATagAttr tbaaAttr) const;

379

380

381

382 LogicalResult createTBAAMetadata();

383

384

385 LogicalResult createIdentMetadata();

386

387

388 LogicalResult createCommandlineMetadata();

389

390

391 LogicalResult createDependentLibrariesMetadata();

392

393

394 LogicalResult

395 convertDialectAttributes(Operation *op,

397

398

399

400 FailureOrllvm::AttrBuilder

402

403

405 std::unique_ptrllvm::Module llvmModule;

406

407 std::unique_ptrdetail::DebugTranslation debugTranslation;

408

409

410 std::unique_ptrdetail::LoopAnnotationTranslation loopAnnotationTranslation;

411

412

413 std::unique_ptrllvm::OpenMPIRBuilder ompBuilder;

414

415

417

418

419

421

422

424

425

426

428

429

430 llvm::StringMap<llvm::Function *> functionMapping;

433

434

435

436

438

439

440

441

443

444

445

447

448

449

451

452

453

455

456

457

459

460

461

462

464

465

466

468

469

470

472

473

475 };

476

477 namespace detail {

478

479

480

482

483

484

485

486

487

490 const ModuleTranslation &moduleTranslation);

491

492

496 ArrayRef<llvm::Type *> tys = {});

497

498

499

500

502 llvm::IRBuilderBase &builder, ModuleTranslation &moduleTranslation,

504 ArrayRef overloadedResults, ArrayRef overloadedOperands,

505 ArrayRef immArgPositions,

506 ArrayRef immArgAttrNames);

507

508 }

509

510 }

511 }

512

513 namespace llvm {

514 template

516 static inline bool

517 doit(const ::mlir::LLVM::ModuleTranslation::StackFrame &frame) {

518 return frame.getTypeID() == ::mlir::TypeID::get();

519 }

520 };

521 }

522

523 #endif

Attributes are known-constant values of operations.

Block represents an ordered list of Operations.

Interface collection for translation to LLVM IR, dispatches to a concrete interface implementation ba...

This class represents the base attribute for all debug info attributes.

Concrete CRTP base class for ModuleTranslation stack frames.

Common CRTP base class for ModuleTranslation stack frames.

virtual ~StackFrame()=default

StackFrame(TypeID typeID)

Implementation class for module translation.

void mapUnresolvedBlockAddress(BlockAddressOp op, llvm::Value *cst)

Maps a blockaddress operation to its corresponding placeholder LLVM value.

llvm::fp::ExceptionBehavior translateFPExceptionBehavior(LLVM::FPExceptionBehavior exceptionBehavior)

Translates the given LLVM FP exception behavior metadata.

llvm::Value * lookupValue(Value value) const

Finds an LLVM IR value corresponding to the given MLIR value.

void mapCall(Operation *mlir, llvm::CallInst *llvm)

Stores a mapping between an MLIR call operation and a corresponding LLVM call instruction.

llvm::DIGlobalVariableExpression * translateGlobalVariableExpression(LLVM::DIGlobalVariableExpressionAttr attr)

Translates the given LLVM global variable expression metadata.

llvm::Value *& mapValue(Value value)

Provides write-once access to store the LLVM IR value corresponding to the given MLIR value.

FailureOr< llvm::AttrBuilder > convertParameterAttrs(mlir::Location loc, DictionaryAttr paramAttrs)

Translates parameter attributes of a call and adds them to the returned AttrBuilder.

WalkResult stackWalk(llvm::function_ref< WalkResult(T &)> callback)

Calls callback for every ModuleTranslation stack frame of type T starting from the top of the stack.

void stackPush(Args &&...args)

Creates a stack frame of type T on ModuleTranslation stack.

llvm::NamedMDNode * getOrInsertNamedModuleMetadata(StringRef name)

Gets the named metadata in the LLVM IR module being constructed, creating it if it does not exist.

LogicalResult convertBlock(Block &bb, bool ignoreArguments, llvm::IRBuilderBase &builder)

Translates the contents of the given block to LLVM IR using this translator.

void mapBranch(Operation *mlir, llvm::Instruction *llvm)

Stores the mapping between an MLIR operation with successors and a corresponding LLVM IR instruction.

llvm::Instruction * lookupBranch(Operation *op) const

Finds an LLVM IR instruction that corresponds to the given MLIR operation with successors.

SmallVector< llvm::Value * > lookupValues(ValueRange values)

Looks up remapped a list of remapped values.

void mapFunction(StringRef name, llvm::Function *func)

Stores the mapping between a function name and its LLVM IR representation.

llvm::DILocation * translateLoc(Location loc, llvm::DILocalScope *scope)

Translates the given location.

llvm::BasicBlock * lookupBlock(Block *block) const

Finds an LLVM IR basic block that corresponds to the given MLIR block.

void setDereferenceableMetadata(DereferenceableOpInterface op, llvm::Instruction *inst)

Sets LLVM dereferenceable metadata for operations that have dereferenceable attributes.

SymbolTableCollection & symbolTable()

void setBranchWeightsMetadata(BranchWeightOpInterface op)

Sets LLVM profiling metadata for operations that have branch weights.

llvm::Type * convertType(Type type)

Converts the type from MLIR LLVM dialect to LLVM.

llvm::GlobalValue * lookupAlias(Operation *op)

Finds an LLVM IR global value that corresponds to the given MLIR operation defining a global alias va...

llvm::RoundingMode translateRoundingMode(LLVM::RoundingMode rounding)

Translates the given LLVM rounding mode metadata.

void setTBAAMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst)

Sets LLVM TBAA metadata for memory operations that have TBAA attributes.

llvm::DIExpression * translateExpression(LLVM::DIExpressionAttr attr)

Translates the given LLVM DWARF expression metadata.

llvm::OpenMPIRBuilder * getOpenMPBuilder()

Returns the OpenMP IR builder associated with the LLVM IR module being constructed.

llvm::CallInst * lookupCall(Operation *op) const

Finds an LLVM call instruction that corresponds to the given MLIR call operation.

llvm::Metadata * translateDebugInfo(LLVM::DINodeAttr attr)

Translates the given LLVM debug info metadata.

void setDisjointFlag(Operation *op, llvm::Value *value)

Sets the disjoint flag attribute for the exported instruction value given the original operation op.

llvm::LLVMContext & getLLVMContext() const

Returns the LLVM context in which the IR is being constructed.

llvm::GlobalValue * lookupGlobal(Operation *op)

Finds an LLVM IR global value that corresponds to the given MLIR operation defining a global value.

llvm::Module * getLLVMModule()

Returns the LLVM module in which the IR is being constructed.

llvm::Function * lookupFunction(StringRef name) const

Finds an LLVM IR function by its name.

llvm::BasicBlock * lookupBlockAddress(BlockAddressAttr attr) const

Finds the LLVM basic block that corresponds to the given BlockAddressAttr.

llvm::MDNode * getOrCreateAliasScopes(ArrayRef< AliasScopeAttr > aliasScopeAttrs)

Returns the LLVM metadata corresponding to an array of mlir LLVM dialect alias scope attributes.

void mapBlock(Block *mlir, llvm::BasicBlock *llvm)

Stores the mapping between an MLIR block and LLVM IR basic block.

llvm::MDNode * getOrCreateAliasScope(AliasScopeAttr aliasScopeAttr)

Returns the LLVM metadata corresponding to a mlir LLVM dialect alias scope attribute.

void stackPop()

Pops the last element from the ModuleTranslation stack.

void forgetMapping(Region &region)

Removes the mapping for blocks contained in the region and values defined in these blocks.

void setAliasScopeMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst)

void setAccessGroupsMetadata(AccessGroupOpInterface op, llvm::Instruction *inst)

MLIRContext & getContext()

Returns the MLIR context of the module being translated.

void mapValue(Value mlir, llvm::Value *llvm)

Stores the mapping between an MLIR value and its LLVM IR counterpart.

void mapBlockAddress(BlockAddressAttr attr, llvm::BasicBlock *block)

Maps a BlockAddressAttr to its corresponding LLVM basic block.

void setLoopMetadata(Operation *op, llvm::Instruction *inst)

Sets LLVM loop metadata for branch operations that have a loop annotation attribute.

Utility class to translate MLIR LLVM dialect types to LLVM IR.

This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...

MLIRContext is the top-level object for a collection of MLIR operations.

Operation is the basic unit of execution within MLIR.

MLIRContext * getContext()

Return the context this operation is associated with.

This class contains a list of basic blocks and a link to the parent operation it is attached to.

This class represents a collection of SymbolTables.

This class provides an efficient unique identifier for a specific C++ type.

Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...

This class provides an abstraction over the different types of ranges over Values.

This class represents an instance of an SSA value in the MLIR system, representing a computable value...

A utility result that is used to signal how to proceed with an ongoing walk:

static WalkResult advance()

bool wasInterrupted() const

Returns true if the walk was interrupted.

The OpAsmOpInterface, see OpAsmInterface.td for more details.

void connectPHINodes(Region &region, const ModuleTranslation &state)

For all blocks in the region that were converted to LLVM IR using the given ModuleTranslation,...

llvm::CallInst * createIntrinsicCall(llvm::IRBuilderBase &builder, llvm::Intrinsic::ID intrinsic, ArrayRef< llvm::Value * > args={}, ArrayRef< llvm::Type * > tys={})

Creates a call to an LLVM IR intrinsic function with the given arguments.

llvm::Constant * getLLVMConstant(llvm::Type *llvmType, Attribute attr, Location loc, const ModuleTranslation &moduleTranslation)

Create an LLVM IR constant of llvmType from the MLIR attribute attr.

Include the generated interface declarations.

std::unique_ptr< llvm::Module > translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext, llvm::StringRef name="LLVMDialectModule", bool disableVerification=false)

Translates a given LLVM dialect module into an LLVM IR module living in the given context.

auto get(MLIRContext *context, Ts &&...params)

Helper method that injects context only if needed, this helps unify some of the attribute constructio...

static bool doit(const ::mlir::LLVM::ModuleTranslation::StackFrame &frame)

RAII object calling stackPush/stackPop on construction/destruction.

SaveStack(ModuleTranslation &m, Args &&...args)