MLIR  20.0.0git
ToLLVMInterface.cpp
Go to the documentation of this file.
1 //===- ToLLVMInterface.cpp - MLIR LLVM Conversion -------------------------===//
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 
10 #include "mlir/IR/Dialect.h"
11 #include "mlir/IR/Operation.h"
12 #include "llvm/ADT/DenseSet.h"
13 
14 using namespace mlir;
15 
17  Operation *root, ConversionTarget &target, LLVMTypeConverter &typeConverter,
19  DenseSet<Dialect *> dialects;
20  root->walk([&](Operation *op) {
21  Dialect *dialect = op->getDialect();
22  if (!dialects.insert(dialect).second)
23  return;
24  // First time we encounter this dialect: if it implements the interface,
25  // let's populate patterns !
26  auto *iface = dyn_cast<ConvertToLLVMPatternInterface>(dialect);
27  if (!iface)
28  return;
29  iface->populateConvertToLLVMConversionPatterns(target, typeConverter,
30  patterns);
31  });
32 }
33 
35  Operation *op, ConversionTarget &target, LLVMTypeConverter &typeConverter,
37  auto iface = dyn_cast<ConvertToLLVMOpInterface>(op);
38  if (!iface)
39  iface = op->getParentOfType<ConvertToLLVMOpInterface>();
40  if (!iface)
41  return;
43  iface.getConvertToLLVMConversionAttrs(attrs);
44  for (ConvertToLLVMAttrInterface attr : attrs)
45  attr.populateConvertToLLVMConversionPatterns(target, typeConverter,
46  patterns);
47 }
48 
49 #include "mlir/Conversion/ConvertToLLVM/ToLLVMAttrInterface.cpp.inc"
50 
51 #include "mlir/Conversion/ConvertToLLVM/ToLLVMOpInterface.cpp.inc"
This class describes a specific conversion target.
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition: Dialect.h:38
Conversion from types to the LLVM IR dialect.
Definition: TypeConverter.h:35
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Dialect * getDialect()
Return the dialect this operation is associated with, or nullptr if the associated dialect is not loa...
Definition: Operation.h:220
std::enable_if_t< llvm::function_traits< std::decay_t< FnT > >::num_args==1, RetT > walk(FnT &&callback)
Walk the operation by calling the callback for each nested operation (including this one),...
Definition: Operation.h:798
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
Definition: Operation.h:238
Include the generated interface declarations.
void populateConversionTargetFromOperation(Operation *op, ConversionTarget &target, LLVMTypeConverter &typeConverter, RewritePatternSet &patterns)
Recursively walk the IR and collect all dialects implementing the interface, and populate the convers...
const FrozenRewritePatternSet & patterns
void populateOpConvertToLLVMConversionPatterns(Operation *op, ConversionTarget &target, LLVMTypeConverter &typeConverter, RewritePatternSet &patterns)
Helper function for populating LLVM conversion patterns.