xref: /llvm-project/flang/lib/Optimizer/Dialect/FIRDialect.cpp (revision 137bd782959523e8168c346bc8801d0b14f684c5)
1 //===-- FIRDialect.cpp ----------------------------------------------------===//
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 // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "flang/Optimizer/Dialect/FIRDialect.h"
14 #include "flang/Optimizer/Dialect/FIRAttr.h"
15 #include "flang/Optimizer/Dialect/FIROps.h"
16 #include "flang/Optimizer/Dialect/FIRType.h"
17 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
18 #include "mlir/Target/LLVMIR/ModuleTranslation.h"
19 #include "mlir/Transforms/InliningUtils.h"
20 
21 using namespace fir;
22 
23 namespace {
24 /// This class defines the interface for handling inlining of FIR calls.
25 struct FIRInlinerInterface : public mlir::DialectInlinerInterface {
26   using DialectInlinerInterface::DialectInlinerInterface;
27 
28   bool isLegalToInline(mlir::Operation *call, mlir::Operation *callable,
29                        bool wouldBeCloned) const final {
30     return fir::canLegallyInline(call, callable, wouldBeCloned);
31   }
32 
33   /// This hook checks to see if the operation `op` is legal to inline into the
34   /// given region `reg`.
35   bool isLegalToInline(mlir::Operation *op, mlir::Region *reg,
36                        bool wouldBeCloned, mlir::IRMapping &map) const final {
37     return fir::canLegallyInline(op, reg, wouldBeCloned, map);
38   }
39 
40   /// This hook is called when a terminator operation has been inlined.
41   /// We handle the return (a Fortran FUNCTION) by replacing the values
42   /// previously returned by the call operation with the operands of the
43   /// return.
44   void handleTerminator(mlir::Operation *op,
45                         mlir::ValueRange valuesToRepl) const final {
46     auto returnOp = llvm::cast<mlir::func::ReturnOp>(op);
47     assert(returnOp.getNumOperands() == valuesToRepl.size());
48     for (const auto &it : llvm::enumerate(returnOp.getOperands()))
49       valuesToRepl[it.index()].replaceAllUsesWith(it.value());
50   }
51 
52   mlir::Operation *materializeCallConversion(mlir::OpBuilder &builder,
53                                              mlir::Value input,
54                                              mlir::Type resultType,
55                                              mlir::Location loc) const final {
56     return builder.create<fir::ConvertOp>(loc, resultType, input);
57   }
58 };
59 } // namespace
60 
61 fir::FIROpsDialect::FIROpsDialect(mlir::MLIRContext *ctx)
62     : mlir::Dialect("fir", ctx, mlir::TypeID::get<FIROpsDialect>()) {
63   getContext()->loadDialect<mlir::LLVM::LLVMDialect>();
64   registerTypes();
65   registerAttributes();
66   addOperations<
67 #define GET_OP_LIST
68 #include "flang/Optimizer/Dialect/FIROps.cpp.inc"
69       >();
70   registerOpExternalInterfaces();
71 }
72 
73 // Register the FIRInlinerInterface to FIROpsDialect
74 void fir::addFIRInlinerExtension(mlir::DialectRegistry &registry) {
75   registry.addExtension(
76       +[](mlir::MLIRContext *ctx, fir::FIROpsDialect *dialect) {
77         dialect->addInterface<FIRInlinerInterface>();
78       });
79 }
80 
81 // We do not provide LLVMTranslationDialectInterface implementation
82 // for FIR dialect, since at the point of translation to LLVM IR
83 // there should not be any FIR operations (the CodeGen converts
84 // them to LLVMIR dialect operations).
85 // Here we register the default implementation of
86 // LLVMTranslationDialectInterface that will drop all FIR dialect
87 // attributes - this helps to avoid warnings about unhandled attributes.
88 // We can provide our own implementation of the interface,
89 // when more sophisticated translation is required.
90 void fir::addFIRToLLVMIRExtension(mlir::DialectRegistry &registry) {
91   registry.addExtension(
92       +[](mlir::MLIRContext *ctx, fir::FIROpsDialect *dialect) {
93         dialect->addInterface<mlir::LLVMTranslationDialectInterface>();
94       });
95 }
96 
97 // anchor the class vtable to this compilation unit
98 fir::FIROpsDialect::~FIROpsDialect() {
99   // do nothing
100 }
101 
102 mlir::Type fir::FIROpsDialect::parseType(mlir::DialectAsmParser &parser) const {
103   return parseFirType(const_cast<FIROpsDialect *>(this), parser);
104 }
105 
106 void fir::FIROpsDialect::printType(mlir::Type ty,
107                                    mlir::DialectAsmPrinter &p) const {
108   return printFirType(const_cast<FIROpsDialect *>(this), ty, p);
109 }
110 
111 mlir::Attribute
112 fir::FIROpsDialect::parseAttribute(mlir::DialectAsmParser &parser,
113                                    mlir::Type type) const {
114   return parseFirAttribute(const_cast<FIROpsDialect *>(this), parser, type);
115 }
116 
117 void fir::FIROpsDialect::printAttribute(mlir::Attribute attr,
118                                         mlir::DialectAsmPrinter &p) const {
119   printFirAttribute(const_cast<FIROpsDialect *>(this), attr, p);
120 }
121