Since the very first commits, the Python and C MLIR APIs have had mis-placed registration/load functionality for dialects, extensions, etc. This was done pragmatically in order to get bootstrapped and then just grew in. Downstreams largely bypass and do their own thing by providing various APIs to register things they need. Meanwhile, the C++ APIs have stabilized around this and it would make sense to follow suit.
The thing we have observed in canonical usage by downstreams is that each downstream tends to have native entry points that configure its installation to its preferences with one-stop APIs. This patch leans in to this approach with `RegisterEverything.h` and `mlir._mlir_libs._mlirRegisterEverything` being the one-stop entry points for the "upstream packages". The `_mlir_libs.__init__.py` now allows customization of the environment and Context by adding "initialization modules" to the `_mlir_libs` package. If present, `_mlirRegisterEverything` is treated as such a module. Others can be added by downstreams by adding a `_site_initialize_{i}.py` module, where '{i}' is a number starting with zero. The number will be incremented and corresponding module loaded until one is not found. Initialization modules can:
* Perform load time customization to the global environment (i.e. registering passes, hooks, etc).
* Define a `register_dialects(registry: DialectRegistry)` function that can extend the `DialectRegistry` that will be used to bootstrap the `Context`.
* Define a `context_init_hook(context: Context)` function that will be added to a list of callbacks which will be invoked after dialect registration during `Context` initialization.
Note that the `MLIRPythonExtension.RegisterEverything` is not included by default when building a downstream (its corresponding behavior was prior). For downstreams which need the default MLIR initialization to take place, they must add this back in to their Python CMake build just like they add their own components (i.e. to `add_mlir_python_common_capi_library` and `add_mlir_python_modules`). It is perfectly valid to not do this, in which case, only the things explicitly depended on and initialized by downstreams will be built/packaged. If the downstream has not been set up for this, it is recommended to simply add this back for the time being and pay the build time/package size cost.
CMake changes:
* `MLIRCAPIRegistration` -> `MLIRCAPIRegisterEverything` (renamed to signify what it does and force an evaluation: a number of places were incidentally linking this very expensive target)
* `MLIRPythonSoure.Passes` removed (without replacement: just drop)
* `MLIRPythonExtension.AllPassesRegistration` removed (without replacement: just drop)
* `MLIRPythonExtension.Conversions` removed (without replacement: just drop)
* `MLIRPythonExtension.Transforms` removed (without replacement: just drop)
Header changes:
* `mlir-c/Registration.h` is deleted. Dialect registration functionality is now in `IR.h`. Registration of upstream features are in `mlir-c/RegisterEverything.h`. When updating MLIR and a couple of downstreams, I found that proper usage was commingled so required making a choice vs just blind S&R.
Python APIs removed:
* mlir.transforms and mlir.conversions (previously only had an __init__.py which indirectly triggered `mlirRegisterTransformsPasses()` and `mlirRegisterConversionPasses()` respectively). Downstream impact: Remove these imports if present (they now happen as part of default initialization).
* mlir._mlir_libs._all_passes_registration, mlir._mlir_libs._mlirTransforms, mlir._mlir_libs._mlirConversions. Downstream impact: None expected (these were internally used).
C-APIs changed:
* mlirRegisterAllDialects(MlirContext) now takes an MlirDialectRegistry instead. It also used to trigger loading of all dialects, which was already marked with a TODO to remove -- it no longer does, and for direct use, dialects must be explicitly loaded. Downstream impact: Direct C-API users must ensure that needed dialects are loaded or call `mlirContextLoadAllAvailableDialects(MlirContext)` to emulate the prior behavior. Also see the `ir.c` test case (e.g. ` mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("func"));`).
* mlirDialectHandle* APIs were moved from Registration.h (which now is restricted to just global/upstream registration) to IR.h, arguably where it should have been. Downstream impact: include correct header (likely already doing so).
C-APIs added:
* mlirContextLoadAllAvailableDialects(MlirContext): Corresponds to C++ API with the same purpose.
Python APIs added:
* mlir.ir.DialectRegistry: Mapping for an MlirDialectRegistry.
* mlir.ir.Context.append_dialect_registry(MlirDialectRegistry)
* mlir.ir.Context.load_all_available_dialects()
* mlir._mlir_libs._mlirAllRegistration: New native extension that exposes a `register_dialects(MlirDialectRegistry)` entry point and performs all upstream pass/conversion/transforms registration on init. In this first step, we eagerly load this as part of the __init__.py and use it to monkey patch the Context to emulate prior behavior.
* Type caster and capsule support for MlirDialectRegistry
This should make it possible to build downstream Python dialects that only depend on a subset of MLIR. See: https://github.com/llvm/llvm-project/issues/56037
Here is an example PR, minimally adapting IREE to these changes: https://github.com/iree-org/iree/pull/9638/files In this situation, IREE is opting to not link everything, since it is already configuring the Context to its liking. For projects that would just like to not think about it and pull in everything, add `MLIRPythonExtension.RegisterEverything` to the list of Python sources getting built, and the old behavior will continue.
Reviewed By: mehdi_amini, ftynse
Differential Revision: https://reviews.llvm.org/D128593
536 lines
19 KiB
C
536 lines
19 KiB
C
//===- pass.c - Simple test of C APIs -------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM
|
|
// Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/* RUN: mlir-capi-pass-test 2>&1 | FileCheck %s
|
|
*/
|
|
|
|
#include "mlir-c/Pass.h"
|
|
#include "mlir-c/Dialect/Func.h"
|
|
#include "mlir-c/IR.h"
|
|
#include "mlir-c/RegisterEverything.h"
|
|
#include "mlir-c/Transforms.h"
|
|
|
|
#include <assert.h>
|
|
#include <math.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static void registerAllUpstreamDialects(MlirContext ctx) {
|
|
MlirDialectRegistry registry = mlirDialectRegistryCreate();
|
|
mlirRegisterAllDialects(registry);
|
|
mlirContextAppendDialectRegistry(ctx, registry);
|
|
mlirDialectRegistryDestroy(registry);
|
|
}
|
|
|
|
void testRunPassOnModule() {
|
|
MlirContext ctx = mlirContextCreate();
|
|
registerAllUpstreamDialects(ctx);
|
|
|
|
MlirModule module = mlirModuleCreateParse(
|
|
ctx,
|
|
// clang-format off
|
|
mlirStringRefCreateFromCString(
|
|
"func.func @foo(%arg0 : i32) -> i32 { \n"
|
|
" %res = arith.addi %arg0, %arg0 : i32 \n"
|
|
" return %res : i32 \n"
|
|
"}"));
|
|
// clang-format on
|
|
if (mlirModuleIsNull(module)) {
|
|
fprintf(stderr, "Unexpected failure parsing module.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
// Run the print-op-stats pass on the top-level module:
|
|
// CHECK-LABEL: Operations encountered:
|
|
// CHECK: arith.addi , 1
|
|
// CHECK: func.func , 1
|
|
// CHECK: func.return , 1
|
|
{
|
|
MlirPassManager pm = mlirPassManagerCreate(ctx);
|
|
MlirPass printOpStatPass = mlirCreateTransformsPrintOpStats();
|
|
mlirPassManagerAddOwnedPass(pm, printOpStatPass);
|
|
MlirLogicalResult success = mlirPassManagerRun(pm, module);
|
|
if (mlirLogicalResultIsFailure(success)) {
|
|
fprintf(stderr, "Unexpected failure running pass manager.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
mlirPassManagerDestroy(pm);
|
|
}
|
|
mlirModuleDestroy(module);
|
|
mlirContextDestroy(ctx);
|
|
}
|
|
|
|
void testRunPassOnNestedModule() {
|
|
MlirContext ctx = mlirContextCreate();
|
|
registerAllUpstreamDialects(ctx);
|
|
|
|
MlirModule module = mlirModuleCreateParse(
|
|
ctx,
|
|
// clang-format off
|
|
mlirStringRefCreateFromCString(
|
|
"func.func @foo(%arg0 : i32) -> i32 { \n"
|
|
" %res = arith.addi %arg0, %arg0 : i32 \n"
|
|
" return %res : i32 \n"
|
|
"} \n"
|
|
"module { \n"
|
|
" func.func @bar(%arg0 : f32) -> f32 { \n"
|
|
" %res = arith.addf %arg0, %arg0 : f32 \n"
|
|
" return %res : f32 \n"
|
|
" } \n"
|
|
"}"));
|
|
// clang-format on
|
|
if (mlirModuleIsNull(module))
|
|
exit(1);
|
|
|
|
// Run the print-op-stats pass on functions under the top-level module:
|
|
// CHECK-LABEL: Operations encountered:
|
|
// CHECK: arith.addi , 1
|
|
// CHECK: func.func , 1
|
|
// CHECK: func.return , 1
|
|
{
|
|
MlirPassManager pm = mlirPassManagerCreate(ctx);
|
|
MlirOpPassManager nestedFuncPm = mlirPassManagerGetNestedUnder(
|
|
pm, mlirStringRefCreateFromCString("func.func"));
|
|
MlirPass printOpStatPass = mlirCreateTransformsPrintOpStats();
|
|
mlirOpPassManagerAddOwnedPass(nestedFuncPm, printOpStatPass);
|
|
MlirLogicalResult success = mlirPassManagerRun(pm, module);
|
|
if (mlirLogicalResultIsFailure(success))
|
|
exit(2);
|
|
mlirPassManagerDestroy(pm);
|
|
}
|
|
// Run the print-op-stats pass on functions under the nested module:
|
|
// CHECK-LABEL: Operations encountered:
|
|
// CHECK: arith.addf , 1
|
|
// CHECK: func.func , 1
|
|
// CHECK: func.return , 1
|
|
{
|
|
MlirPassManager pm = mlirPassManagerCreate(ctx);
|
|
MlirOpPassManager nestedModulePm = mlirPassManagerGetNestedUnder(
|
|
pm, mlirStringRefCreateFromCString("builtin.module"));
|
|
MlirOpPassManager nestedFuncPm = mlirOpPassManagerGetNestedUnder(
|
|
nestedModulePm, mlirStringRefCreateFromCString("func.func"));
|
|
MlirPass printOpStatPass = mlirCreateTransformsPrintOpStats();
|
|
mlirOpPassManagerAddOwnedPass(nestedFuncPm, printOpStatPass);
|
|
MlirLogicalResult success = mlirPassManagerRun(pm, module);
|
|
if (mlirLogicalResultIsFailure(success))
|
|
exit(2);
|
|
mlirPassManagerDestroy(pm);
|
|
}
|
|
|
|
mlirModuleDestroy(module);
|
|
mlirContextDestroy(ctx);
|
|
}
|
|
|
|
static void printToStderr(MlirStringRef str, void *userData) {
|
|
(void)userData;
|
|
fwrite(str.data, 1, str.length, stderr);
|
|
}
|
|
|
|
void testPrintPassPipeline() {
|
|
MlirContext ctx = mlirContextCreate();
|
|
MlirPassManager pm = mlirPassManagerCreate(ctx);
|
|
// Populate the pass-manager
|
|
MlirOpPassManager nestedModulePm = mlirPassManagerGetNestedUnder(
|
|
pm, mlirStringRefCreateFromCString("builtin.module"));
|
|
MlirOpPassManager nestedFuncPm = mlirOpPassManagerGetNestedUnder(
|
|
nestedModulePm, mlirStringRefCreateFromCString("func.func"));
|
|
MlirPass printOpStatPass = mlirCreateTransformsPrintOpStats();
|
|
mlirOpPassManagerAddOwnedPass(nestedFuncPm, printOpStatPass);
|
|
|
|
// Print the top level pass manager
|
|
// CHECK: Top-level: builtin.module(func.func(print-op-stats{json=false}))
|
|
fprintf(stderr, "Top-level: ");
|
|
mlirPrintPassPipeline(mlirPassManagerGetAsOpPassManager(pm), printToStderr,
|
|
NULL);
|
|
fprintf(stderr, "\n");
|
|
|
|
// Print the pipeline nested one level down
|
|
// CHECK: Nested Module: func.func(print-op-stats{json=false})
|
|
fprintf(stderr, "Nested Module: ");
|
|
mlirPrintPassPipeline(nestedModulePm, printToStderr, NULL);
|
|
fprintf(stderr, "\n");
|
|
|
|
// Print the pipeline nested two levels down
|
|
// CHECK: Nested Module>Func: print-op-stats
|
|
fprintf(stderr, "Nested Module>Func: ");
|
|
mlirPrintPassPipeline(nestedFuncPm, printToStderr, NULL);
|
|
fprintf(stderr, "\n");
|
|
|
|
mlirPassManagerDestroy(pm);
|
|
mlirContextDestroy(ctx);
|
|
}
|
|
|
|
void testParsePassPipeline() {
|
|
MlirContext ctx = mlirContextCreate();
|
|
MlirPassManager pm = mlirPassManagerCreate(ctx);
|
|
// Try parse a pipeline.
|
|
MlirLogicalResult status = mlirParsePassPipeline(
|
|
mlirPassManagerGetAsOpPassManager(pm),
|
|
mlirStringRefCreateFromCString(
|
|
"builtin.module(func.func(print-op-stats{json=false}),"
|
|
" func.func(print-op-stats{json=false}))"));
|
|
// Expect a failure, we haven't registered the print-op-stats pass yet.
|
|
if (mlirLogicalResultIsSuccess(status)) {
|
|
fprintf(
|
|
stderr,
|
|
"Unexpected success parsing pipeline without registering the pass\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
// Try again after registrating the pass.
|
|
mlirRegisterTransformsPrintOpStats();
|
|
status = mlirParsePassPipeline(
|
|
mlirPassManagerGetAsOpPassManager(pm),
|
|
mlirStringRefCreateFromCString(
|
|
"builtin.module(func.func(print-op-stats{json=false}),"
|
|
" func.func(print-op-stats{json=false}))"));
|
|
// Expect a failure, we haven't registered the print-op-stats pass yet.
|
|
if (mlirLogicalResultIsFailure(status)) {
|
|
fprintf(stderr,
|
|
"Unexpected failure parsing pipeline after registering the pass\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
// CHECK: Round-trip: builtin.module(func.func(print-op-stats{json=false}),
|
|
// func.func(print-op-stats{json=false}))
|
|
fprintf(stderr, "Round-trip: ");
|
|
mlirPrintPassPipeline(mlirPassManagerGetAsOpPassManager(pm), printToStderr,
|
|
NULL);
|
|
fprintf(stderr, "\n");
|
|
mlirPassManagerDestroy(pm);
|
|
mlirContextDestroy(ctx);
|
|
}
|
|
|
|
struct TestExternalPassUserData {
|
|
int constructCallCount;
|
|
int destructCallCount;
|
|
int initializeCallCount;
|
|
int cloneCallCount;
|
|
int runCallCount;
|
|
};
|
|
typedef struct TestExternalPassUserData TestExternalPassUserData;
|
|
|
|
void testConstructExternalPass(void *userData) {
|
|
++((TestExternalPassUserData *)userData)->constructCallCount;
|
|
}
|
|
|
|
void testDestructExternalPass(void *userData) {
|
|
++((TestExternalPassUserData *)userData)->destructCallCount;
|
|
}
|
|
|
|
MlirLogicalResult testInitializeExternalPass(MlirContext ctx, void *userData) {
|
|
++((TestExternalPassUserData *)userData)->initializeCallCount;
|
|
return mlirLogicalResultSuccess();
|
|
}
|
|
|
|
MlirLogicalResult testInitializeFailingExternalPass(MlirContext ctx,
|
|
void *userData) {
|
|
++((TestExternalPassUserData *)userData)->initializeCallCount;
|
|
return mlirLogicalResultFailure();
|
|
}
|
|
|
|
void *testCloneExternalPass(void *userData) {
|
|
++((TestExternalPassUserData *)userData)->cloneCallCount;
|
|
return userData;
|
|
}
|
|
|
|
void testRunExternalPass(MlirOperation op, MlirExternalPass pass,
|
|
void *userData) {
|
|
++((TestExternalPassUserData *)userData)->runCallCount;
|
|
}
|
|
|
|
void testRunExternalFuncPass(MlirOperation op, MlirExternalPass pass,
|
|
void *userData) {
|
|
++((TestExternalPassUserData *)userData)->runCallCount;
|
|
MlirStringRef opName = mlirIdentifierStr(mlirOperationGetName(op));
|
|
if (!mlirStringRefEqual(opName,
|
|
mlirStringRefCreateFromCString("func.func"))) {
|
|
mlirExternalPassSignalFailure(pass);
|
|
}
|
|
}
|
|
|
|
void testRunFailingExternalPass(MlirOperation op, MlirExternalPass pass,
|
|
void *userData) {
|
|
++((TestExternalPassUserData *)userData)->runCallCount;
|
|
mlirExternalPassSignalFailure(pass);
|
|
}
|
|
|
|
MlirExternalPassCallbacks makeTestExternalPassCallbacks(
|
|
MlirLogicalResult (*initializePass)(MlirContext ctx, void *userData),
|
|
void (*runPass)(MlirOperation op, MlirExternalPass, void *userData)) {
|
|
return (MlirExternalPassCallbacks){testConstructExternalPass,
|
|
testDestructExternalPass, initializePass,
|
|
testCloneExternalPass, runPass};
|
|
}
|
|
|
|
void testExternalPass() {
|
|
MlirContext ctx = mlirContextCreate();
|
|
registerAllUpstreamDialects(ctx);
|
|
|
|
MlirModule module = mlirModuleCreateParse(
|
|
ctx,
|
|
// clang-format off
|
|
mlirStringRefCreateFromCString(
|
|
"func.func @foo(%arg0 : i32) -> i32 { \n"
|
|
" %res = arith.addi %arg0, %arg0 : i32 \n"
|
|
" return %res : i32 \n"
|
|
"}"));
|
|
// clang-format on
|
|
if (mlirModuleIsNull(module)) {
|
|
fprintf(stderr, "Unexpected failure parsing module.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
MlirStringRef description = mlirStringRefCreateFromCString("");
|
|
MlirStringRef emptyOpName = mlirStringRefCreateFromCString("");
|
|
|
|
MlirTypeIDAllocator typeIDAllocator = mlirTypeIDAllocatorCreate();
|
|
|
|
// Run a generic pass
|
|
{
|
|
MlirTypeID passID = mlirTypeIDAllocatorAllocateTypeID(typeIDAllocator);
|
|
MlirStringRef name = mlirStringRefCreateFromCString("TestExternalPass");
|
|
MlirStringRef argument =
|
|
mlirStringRefCreateFromCString("test-external-pass");
|
|
TestExternalPassUserData userData = {0};
|
|
|
|
MlirPass externalPass = mlirCreateExternalPass(
|
|
passID, name, argument, description, emptyOpName, 0, NULL,
|
|
makeTestExternalPassCallbacks(NULL, testRunExternalPass), &userData);
|
|
|
|
if (userData.constructCallCount != 1) {
|
|
fprintf(stderr, "Expected constructCallCount to be 1\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
MlirPassManager pm = mlirPassManagerCreate(ctx);
|
|
mlirPassManagerAddOwnedPass(pm, externalPass);
|
|
MlirLogicalResult success = mlirPassManagerRun(pm, module);
|
|
if (mlirLogicalResultIsFailure(success)) {
|
|
fprintf(stderr, "Unexpected failure running external pass.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
if (userData.runCallCount != 1) {
|
|
fprintf(stderr, "Expected runCallCount to be 1\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
mlirPassManagerDestroy(pm);
|
|
|
|
if (userData.destructCallCount != userData.constructCallCount) {
|
|
fprintf(stderr, "Expected destructCallCount to be equal to "
|
|
"constructCallCount\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
// Run a func operation pass
|
|
{
|
|
MlirTypeID passID = mlirTypeIDAllocatorAllocateTypeID(typeIDAllocator);
|
|
MlirStringRef name = mlirStringRefCreateFromCString("TestExternalFuncPass");
|
|
MlirStringRef argument =
|
|
mlirStringRefCreateFromCString("test-external-func-pass");
|
|
TestExternalPassUserData userData = {0};
|
|
MlirDialectHandle funcHandle = mlirGetDialectHandle__func__();
|
|
MlirStringRef funcOpName = mlirStringRefCreateFromCString("func.func");
|
|
|
|
MlirPass externalPass = mlirCreateExternalPass(
|
|
passID, name, argument, description, funcOpName, 1, &funcHandle,
|
|
makeTestExternalPassCallbacks(NULL, testRunExternalFuncPass),
|
|
&userData);
|
|
|
|
if (userData.constructCallCount != 1) {
|
|
fprintf(stderr, "Expected constructCallCount to be 1\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
MlirPassManager pm = mlirPassManagerCreate(ctx);
|
|
MlirOpPassManager nestedFuncPm =
|
|
mlirPassManagerGetNestedUnder(pm, funcOpName);
|
|
mlirOpPassManagerAddOwnedPass(nestedFuncPm, externalPass);
|
|
MlirLogicalResult success = mlirPassManagerRun(pm, module);
|
|
if (mlirLogicalResultIsFailure(success)) {
|
|
fprintf(stderr, "Unexpected failure running external operation pass.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
// Since this is a nested pass, it can be cloned and run in parallel
|
|
if (userData.cloneCallCount != userData.constructCallCount - 1) {
|
|
fprintf(stderr, "Expected constructCallCount to be 1\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
// The pass should only be run once this there is only one func op
|
|
if (userData.runCallCount != 1) {
|
|
fprintf(stderr, "Expected runCallCount to be 1\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
mlirPassManagerDestroy(pm);
|
|
|
|
if (userData.destructCallCount != userData.constructCallCount) {
|
|
fprintf(stderr, "Expected destructCallCount to be equal to "
|
|
"constructCallCount\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
// Run a pass with `initialize` set
|
|
{
|
|
MlirTypeID passID = mlirTypeIDAllocatorAllocateTypeID(typeIDAllocator);
|
|
MlirStringRef name = mlirStringRefCreateFromCString("TestExternalPass");
|
|
MlirStringRef argument =
|
|
mlirStringRefCreateFromCString("test-external-pass");
|
|
TestExternalPassUserData userData = {0};
|
|
|
|
MlirPass externalPass = mlirCreateExternalPass(
|
|
passID, name, argument, description, emptyOpName, 0, NULL,
|
|
makeTestExternalPassCallbacks(testInitializeExternalPass,
|
|
testRunExternalPass),
|
|
&userData);
|
|
|
|
if (userData.constructCallCount != 1) {
|
|
fprintf(stderr, "Expected constructCallCount to be 1\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
MlirPassManager pm = mlirPassManagerCreate(ctx);
|
|
mlirPassManagerAddOwnedPass(pm, externalPass);
|
|
MlirLogicalResult success = mlirPassManagerRun(pm, module);
|
|
if (mlirLogicalResultIsFailure(success)) {
|
|
fprintf(stderr, "Unexpected failure running external pass.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
if (userData.initializeCallCount != 1) {
|
|
fprintf(stderr, "Expected initializeCallCount to be 1\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
if (userData.runCallCount != 1) {
|
|
fprintf(stderr, "Expected runCallCount to be 1\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
mlirPassManagerDestroy(pm);
|
|
|
|
if (userData.destructCallCount != userData.constructCallCount) {
|
|
fprintf(stderr, "Expected destructCallCount to be equal to "
|
|
"constructCallCount\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
// Run a pass that fails during `initialize`
|
|
{
|
|
MlirTypeID passID = mlirTypeIDAllocatorAllocateTypeID(typeIDAllocator);
|
|
MlirStringRef name =
|
|
mlirStringRefCreateFromCString("TestExternalFailingPass");
|
|
MlirStringRef argument =
|
|
mlirStringRefCreateFromCString("test-external-failing-pass");
|
|
TestExternalPassUserData userData = {0};
|
|
|
|
MlirPass externalPass = mlirCreateExternalPass(
|
|
passID, name, argument, description, emptyOpName, 0, NULL,
|
|
makeTestExternalPassCallbacks(testInitializeFailingExternalPass,
|
|
testRunExternalPass),
|
|
&userData);
|
|
|
|
if (userData.constructCallCount != 1) {
|
|
fprintf(stderr, "Expected constructCallCount to be 1\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
MlirPassManager pm = mlirPassManagerCreate(ctx);
|
|
mlirPassManagerAddOwnedPass(pm, externalPass);
|
|
MlirLogicalResult success = mlirPassManagerRun(pm, module);
|
|
if (mlirLogicalResultIsSuccess(success)) {
|
|
fprintf(
|
|
stderr,
|
|
"Expected failure running pass manager on failing external pass.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
if (userData.initializeCallCount != 1) {
|
|
fprintf(stderr, "Expected initializeCallCount to be 1\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
if (userData.runCallCount != 0) {
|
|
fprintf(stderr, "Expected runCallCount to be 0\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
mlirPassManagerDestroy(pm);
|
|
|
|
if (userData.destructCallCount != userData.constructCallCount) {
|
|
fprintf(stderr, "Expected destructCallCount to be equal to "
|
|
"constructCallCount\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
// Run a pass that fails during `run`
|
|
{
|
|
MlirTypeID passID = mlirTypeIDAllocatorAllocateTypeID(typeIDAllocator);
|
|
MlirStringRef name =
|
|
mlirStringRefCreateFromCString("TestExternalFailingPass");
|
|
MlirStringRef argument =
|
|
mlirStringRefCreateFromCString("test-external-failing-pass");
|
|
TestExternalPassUserData userData = {0};
|
|
|
|
MlirPass externalPass = mlirCreateExternalPass(
|
|
passID, name, argument, description, emptyOpName, 0, NULL,
|
|
makeTestExternalPassCallbacks(NULL, testRunFailingExternalPass),
|
|
&userData);
|
|
|
|
if (userData.constructCallCount != 1) {
|
|
fprintf(stderr, "Expected constructCallCount to be 1\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
MlirPassManager pm = mlirPassManagerCreate(ctx);
|
|
mlirPassManagerAddOwnedPass(pm, externalPass);
|
|
MlirLogicalResult success = mlirPassManagerRun(pm, module);
|
|
if (mlirLogicalResultIsSuccess(success)) {
|
|
fprintf(
|
|
stderr,
|
|
"Expected failure running pass manager on failing external pass.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
if (userData.runCallCount != 1) {
|
|
fprintf(stderr, "Expected runCallCount to be 1\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
mlirPassManagerDestroy(pm);
|
|
|
|
if (userData.destructCallCount != userData.constructCallCount) {
|
|
fprintf(stderr, "Expected destructCallCount to be equal to "
|
|
"constructCallCount\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
mlirTypeIDAllocatorDestroy(typeIDAllocator);
|
|
mlirModuleDestroy(module);
|
|
mlirContextDestroy(ctx);
|
|
}
|
|
|
|
int main() {
|
|
testRunPassOnModule();
|
|
testRunPassOnNestedModule();
|
|
testPrintPassPipeline();
|
|
testParsePassPipeline();
|
|
testExternalPass();
|
|
return 0;
|
|
}
|