Skip to content

Commit a5ef51d

Browse files
committed
[mlir] Add support for "promised" interfaces
Promised interfaces allow for a dialect to "promise" the implementation of an interface, i.e. declare that it supports an interface, but have the interface defined in an extension in a library separate from the dialect itself. A promised interface is powerful in that it alerts the user when the interface is attempted to be used (e.g. via cast/dyn_cast/etc.) and the implementation has not yet been provided. This makes the system much more robust against misconfiguration, and ensures that we do not lose the benefit we currently have of defining the interface in the dialect library. Differential Revision: https://reviews.llvm.org/D120368
1 parent fb19fa2 commit a5ef51d

40 files changed

+411
-75
lines changed

flang/include/flang/Optimizer/Support/InitFIR.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "mlir/Conversion/Passes.h"
1919
#include "mlir/Dialect/Affine/Passes.h"
2020
#include "mlir/Dialect/Complex/IR/Complex.h"
21+
#include "mlir/Dialect/Func/Extensions/InlinerExtension.h"
2122
#include "mlir/InitAllDialects.h"
2223
#include "mlir/Pass/Pass.h"
2324
#include "mlir/Pass/PassRegistry.h"
@@ -34,27 +35,39 @@ namespace fir::support {
3435
mlir::vector::VectorDialect, mlir::math::MathDialect, \
3536
mlir::complex::ComplexDialect, mlir::DLTIDialect
3637

38+
#define FLANG_CODEGEN_DIALECT_LIST FIRCodeGenDialect, mlir::LLVM::LLVMDialect
39+
3740
// The definitive list of dialects used by flang.
3841
#define FLANG_DIALECT_LIST \
39-
FLANG_NONCODEGEN_DIALECT_LIST, FIRCodeGenDialect, mlir::LLVM::LLVMDialect
42+
FLANG_NONCODEGEN_DIALECT_LIST, FLANG_CODEGEN_DIALECT_LIST
4043

4144
inline void registerNonCodegenDialects(mlir::DialectRegistry &registry) {
4245
registry.insert<FLANG_NONCODEGEN_DIALECT_LIST>();
46+
mlir::func::registerInlinerExtension(registry);
4347
}
4448

4549
/// Register all the dialects used by flang.
4650
inline void registerDialects(mlir::DialectRegistry &registry) {
47-
registry.insert<FLANG_DIALECT_LIST>();
51+
registerNonCodegenDialects(registry);
52+
registry.insert<FLANG_CODEGEN_DIALECT_LIST>();
4853
}
4954

5055
inline void loadNonCodegenDialects(mlir::MLIRContext &context) {
56+
mlir::DialectRegistry registry;
57+
registerNonCodegenDialects(registry);
58+
context.appendDialectRegistry(registry);
59+
5160
context.loadDialect<FLANG_NONCODEGEN_DIALECT_LIST>();
5261
}
5362

5463
/// Forced load of all the dialects used by flang. Lowering is not an MLIR
5564
/// pass, but a producer of FIR and MLIR. It is therefore a requirement that the
5665
/// dialects be preloaded to be able to build the IR.
5766
inline void loadDialects(mlir::MLIRContext &context) {
67+
mlir::DialectRegistry registry;
68+
registerDialects(registry);
69+
context.appendDialectRegistry(registry);
70+
5871
context.loadDialect<FLANG_DIALECT_LIST>();
5972
}
6073

flang/lib/Frontend/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
2+
get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS)
23

34
add_flang_library(flangFrontend
45
CompilerInstance.cpp
@@ -18,6 +19,7 @@ add_flang_library(flangFrontend
1819
HLFIRDialect
1920
MLIRIR
2021
${dialect_libs}
22+
${extension_libs}
2123

2224
LINK_LIBS
2325
FortranParser
@@ -39,6 +41,7 @@ add_flang_library(flangFrontend
3941
MLIRSCFToControlFlow
4042
MLIRTargetLLVMIRImport
4143
${dialect_libs}
44+
${extension_libs}
4245

4346
LINK_COMPONENTS
4447
Passes

flang/lib/Lower/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
2+
get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS)
23

34
add_flang_library(FortranLower
45
Allocatable.cpp
@@ -33,6 +34,7 @@ add_flang_library(FortranLower
3334
FIRTransforms
3435
HLFIRDialect
3536
${dialect_libs}
37+
${extension_libs}
3638

3739
LINK_LIBS
3840
FIRDialect
@@ -42,6 +44,7 @@ add_flang_library(FortranLower
4244
FIRTransforms
4345
HLFIRDialect
4446
${dialect_libs}
47+
${extension_libs}
4548
FortranCommon
4649
FortranParser
4750
FortranEvaluate

flang/lib/Optimizer/Builder/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
2+
get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS)
23

34
add_flang_library(FIRBuilder
45
BoxValue.cpp
@@ -31,11 +32,13 @@ add_flang_library(FIRBuilder
3132
FIRDialect
3233
HLFIRDialect
3334
${dialect_libs}
35+
${extension_libs}
3436

3537
LINK_LIBS
3638
FIRDialect
3739
FIRDialectSupport
3840
FIRSupport
3941
HLFIRDialect
4042
${dialect_libs}
43+
${extension_libs}
4144
)

flang/lib/Optimizer/Support/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
2+
get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS)
23

34
add_flang_library(FIRSupport
45
InitFIR.cpp
@@ -9,9 +10,11 @@ add_flang_library(FIRSupport
910
HLFIROpsIncGen
1011
MLIRIR
1112
${dialect_libs}
13+
${extension_libs}
1214

1315
LINK_LIBS
1416
${dialect_libs}
17+
${extension_libs}
1518
MLIRBuiltinToLLVMIRTranslation
1619
MLIROpenACCToLLVMIRTranslation
1720
MLIROpenMPToLLVMIRTranslation

flang/tools/bbc/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ FIROptCodeGenPassIncGen
1010

1111
llvm_update_compile_flags(bbc)
1212
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
13+
get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS)
1314
target_link_libraries(bbc PRIVATE
1415
FIRDialect
1516
FIRDialectSupport
@@ -19,6 +20,7 @@ FIRBuilder
1920
HLFIRDialect
2021
HLFIRTransforms
2122
${dialect_libs}
23+
${extension_libs}
2224
MLIRAffineToStandard
2325
MLIRSCFToControlFlow
2426
FortranCommon

flang/tools/fir-opt/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_flang_tool(fir-opt fir-opt.cpp)
22
llvm_update_compile_flags(fir-opt)
33
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
4+
get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS)
45

56
if(FLANG_INCLUDE_TESTS)
67
set(test_libs
@@ -18,6 +19,7 @@ target_link_libraries(fir-opt PRIVATE
1819
FIRAnalysis
1920
${test_libs}
2021
${dialect_libs}
22+
${extension_libs}
2123

2224
# TODO: these should be transitive dependencies from a target providing
2325
# "registerFIRPasses()"

flang/tools/tco/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
55
add_flang_tool(tco tco.cpp)
66
llvm_update_compile_flags(tco)
77
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
8+
get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS)
89
target_link_libraries(tco PRIVATE
910
FIRCodeGen
1011
FIRDialect
@@ -15,13 +16,13 @@ target_link_libraries(tco PRIVATE
1516
HLFIRDialect
1617
HLFIRTransforms
1718
${dialect_libs}
19+
${extension_libs}
1820
MLIRIR
1921
MLIRLLVMDialect
2022
MLIRBuiltinToLLVMIRTranslation
2123
MLIRLLVMToLLVMIRTranslation
2224
MLIRTargetLLVMIRExport
2325
MLIRPass
24-
MLIRFuncToLLVM
2526
MLIRTransforms
2627
MLIRAffineToStandard
2728
MLIRAnalysis

flang/unittests/Optimizer/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
2+
get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS)
23

34
set(LIBS
45
FIRBuilder
@@ -8,6 +9,7 @@ set(LIBS
89
FIRSupport
910
HLFIRDialect
1011
${dialect_libs}
12+
${extension_libs}
1113
LLVMTargetParser
1214
)
1315

mlir/cmake/modules/AddMLIR.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,12 @@ function(add_mlir_conversion_library name)
588588
add_mlir_library(${ARGV} DEPENDS mlir-headers)
589589
endfunction(add_mlir_conversion_library)
590590

591+
# Declare the library associated with an extension.
592+
function(add_mlir_extension_library name)
593+
set_property(GLOBAL APPEND PROPERTY MLIR_EXTENSION_LIBS ${name})
594+
add_mlir_library(${ARGV} DEPENDS mlir-headers)
595+
endfunction(add_mlir_extension_library)
596+
591597
# Declare the library associated with a translation.
592598
function(add_mlir_translation_library name)
593599
set_property(GLOBAL APPEND PROPERTY MLIR_TRANSLATION_LIBS ${name})

mlir/cmake/modules/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export(TARGETS ${MLIR_EXPORTS} FILE ${mlir_cmake_builddir}/MLIRTargets.cmake)
2424
get_property(MLIR_ALL_LIBS GLOBAL PROPERTY MLIR_ALL_LIBS)
2525
get_property(MLIR_DIALECT_LIBS GLOBAL PROPERTY MLIR_DIALECT_LIBS)
2626
get_property(MLIR_CONVERSION_LIBS GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
27+
get_property(MLIR_EXTENSION_LIBS GLOBAL PROPERTY MLIR_EXTENSION_LIBS)
2728
get_property(MLIR_TRANSLATION_LIBS GLOBAL PROPERTY MLIR_TRANSLATION_LIBS)
2829

2930
# Generate MlirConfig.cmake for the build tree.

mlir/cmake/modules/MLIRConfig.cmake.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ set(MLIR_MAIN_SRC_DIR "@MLIR_MAIN_SRC_DIR@")
2121
set_property(GLOBAL PROPERTY MLIR_ALL_LIBS "@MLIR_ALL_LIBS@")
2222
set_property(GLOBAL PROPERTY MLIR_DIALECT_LIBS "@MLIR_DIALECT_LIBS@")
2323
set_property(GLOBAL PROPERTY MLIR_CONVERSION_LIBS "@MLIR_CONVERSION_LIBS@")
24+
set_property(GLOBAL PROPERTY MLIR_EXTENSION_LIBS "@MLIR_EXTENSION_LIBS@")
2425
set_property(GLOBAL PROPERTY MLIR_TRANSLATION_LIBS "@MLIR_TRANSLATION_LIBS@")
2526

2627
# Provide all our library targets to users.

mlir/examples/toy/Ch5/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ add_toy_chapter(toyc-ch5
2828
include_directories(${CMAKE_CURRENT_BINARY_DIR})
2929
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include/)
3030
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
31+
get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS)
3132
target_link_libraries(toyc-ch5
3233
PRIVATE
3334
${dialect_libs}
35+
${extension_libs}
3436
MLIRAnalysis
3537
MLIRCallInterfaces
3638
MLIRCastInterfaces

mlir/examples/toy/Ch5/toyc.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "mlir/Dialect/Func/Extensions/AllExtensions.h"
1314
#include "toy/Dialect.h"
1415
#include "toy/MLIRGen.h"
1516
#include "toy/Parser.h"
@@ -107,7 +108,10 @@ int loadMLIR(llvm::SourceMgr &sourceMgr, mlir::MLIRContext &context,
107108
}
108109

109110
int dumpMLIR() {
110-
mlir::MLIRContext context;
111+
mlir::DialectRegistry registry;
112+
mlir::func::registerAllExtensions(registry);
113+
114+
mlir::MLIRContext context(registry);
111115
// Load our Dialect in this MLIR Context.
112116
context.getOrLoadDialect<mlir::toy::ToyDialect>();
113117

mlir/examples/toy/Ch6/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR})
3939
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include/)
4040
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
4141
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
42+
get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS)
4243
target_link_libraries(toyc-ch6
4344
PRIVATE
4445
${dialect_libs}
4546
${conversion_libs}
47+
${extension_libs}
4648
MLIRAnalysis
4749
MLIRBuiltinToLLVMIRTranslation
4850
MLIRCallInterfaces

mlir/examples/toy/Ch6/toyc.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "mlir/Dialect/Func/Extensions/AllExtensions.h"
1314
#include "toy/Dialect.h"
1415
#include "toy/MLIRGen.h"
1516
#include "toy/Parser.h"
@@ -289,8 +290,10 @@ int main(int argc, char **argv) {
289290
return dumpAST();
290291

291292
// If we aren't dumping the AST, then we are compiling with/to MLIR.
293+
mlir::DialectRegistry registry;
294+
mlir::func::registerAllExtensions(registry);
292295

293-
mlir::MLIRContext context;
296+
mlir::MLIRContext context(registry);
294297
// Load our Dialect in this MLIR Context.
295298
context.getOrLoadDialect<mlir::toy::ToyDialect>();
296299

mlir/examples/toy/Ch7/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR})
3838
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include/)
3939
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
4040
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
41+
get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS)
4142
target_link_libraries(toyc-ch7
4243
PRIVATE
4344
${dialect_libs}
4445
${conversion_libs}
46+
${extension_libs}
4547
MLIRAnalysis
4648
MLIRBuiltinToLLVMIRTranslation
4749
MLIRCallInterfaces

mlir/examples/toy/Ch7/toyc.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "mlir/Dialect/Func/Extensions/AllExtensions.h"
1314
#include "toy/Dialect.h"
1415
#include "toy/MLIRGen.h"
1516
#include "toy/Parser.h"
@@ -290,8 +291,10 @@ int main(int argc, char **argv) {
290291
return dumpAST();
291292

292293
// If we aren't dumping the AST, then we are compiling with/to MLIR.
294+
mlir::DialectRegistry registry;
295+
mlir::func::registerAllExtensions(registry);
293296

294-
mlir::MLIRContext context;
297+
mlir::MLIRContext context(registry);
295298
// Load our Dialect in this MLIR Context.
296299
context.getOrLoadDialect<mlir::toy::ToyDialect>();
297300

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===- AllExtensions.h - All Func Extensions --------------------*- C++ -*-===//
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+
// This file defines a common entry point for registering all extensions to the
10+
// func dialect.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef MLIR_DIALECT_FUNC_EXTENSIONS_ALLEXTENSIONS_H
15+
#define MLIR_DIALECT_FUNC_EXTENSIONS_ALLEXTENSIONS_H
16+
17+
namespace mlir {
18+
class DialectRegistry;
19+
20+
namespace func {
21+
/// Register all extensions of the func dialect. This should generally only be
22+
/// used by tools, or other use cases that really do want *all* extensions of
23+
/// the dialect. All other cases should prefer to instead register the specific
24+
/// extensions they intend to take advantage of.
25+
void registerAllExtensions(DialectRegistry &registry);
26+
} // namespace func
27+
28+
} // namespace mlir
29+
30+
#endif // MLIR_DIALECT_FUNC_EXTENSIONS_ALLEXTENSIONS_H
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===- InlinerExtension.h - Func Inliner Extension 0000----------*- C++ -*-===//
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+
// This file defines an extension for the func dialect that implements the
10+
// interfaces necessary to support inlining.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef MLIR_DIALECT_FUNC_EXTENSIONS_INLINEREXTENSION_H
15+
#define MLIR_DIALECT_FUNC_EXTENSIONS_INLINEREXTENSION_H
16+
17+
namespace mlir {
18+
class DialectRegistry;
19+
20+
namespace func {
21+
/// Register the extension used to support inlining the func dialect.
22+
void registerInlinerExtension(DialectRegistry &registry);
23+
} // namespace func
24+
25+
} // namespace mlir
26+
27+
#endif // MLIR_DIALECT_FUNC_EXTENSIONS_INLINEREXTENSION_H

mlir/include/mlir/Dialect/Func/IR/FuncOps.td

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ include "mlir/Interfaces/SideEffectInterfaces.td"
2121
def Func_Dialect : Dialect {
2222
let name = "func";
2323
let cppNamespace = "::mlir::func";
24-
let dependentDialects = ["cf::ControlFlowDialect"];
2524
let hasConstantMaterializer = 1;
2625
let usePropertiesForAttributes = 1;
2726
}

0 commit comments

Comments
 (0)