2019-05-20 16:44:37 +08:00
|
|
|
//===- sdbm-api-test.cpp - Tests for SDBM expression APIs -----------------===//
|
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-05-20 16:44:37 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-05-20 16:44:37 +08:00
|
|
|
|
2019-05-21 22:22:35 +08:00
|
|
|
// RUN: mlir-sdbm-api-test | FileCheck %s
|
2019-05-20 16:44:37 +08:00
|
|
|
|
2019-08-20 02:00:47 +08:00
|
|
|
#include "mlir/Dialect/SDBM/SDBM.h"
|
|
|
|
#include "mlir/Dialect/SDBM/SDBMDialect.h"
|
|
|
|
#include "mlir/Dialect/SDBM/SDBMExpr.h"
|
2019-05-20 16:44:37 +08:00
|
|
|
#include "mlir/IR/MLIRContext.h"
|
|
|
|
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
#include "APITest.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
2020-02-12 17:03:40 +08:00
|
|
|
|
2019-05-20 16:44:37 +08:00
|
|
|
static MLIRContext *ctx() {
|
2020-10-24 04:19:35 +08:00
|
|
|
static thread_local MLIRContext context;
|
Separate the Registration from Loading dialects in the Context
This changes the behavior of constructing MLIRContext to no longer load globally
registered dialects on construction. Instead Dialects are only loaded explicitly
on demand:
- the Parser is lazily loading Dialects in the context as it encounters them
during parsing. This is the only purpose for registering dialects and not load
them in the context.
- Passes are expected to declare the dialects they will create entity from
(Operations, Attributes, or Types), and the PassManager is loading Dialects into
the Context when starting a pipeline.
This changes simplifies the configuration of the registration: a compiler only
need to load the dialect for the IR it will emit, and the optimizer is
self-contained and load the required Dialects. For example in the Toy tutorial,
the compiler only needs to load the Toy dialect in the Context, all the others
(linalg, affine, std, LLVM, ...) are automatically loaded depending on the
optimization pipeline enabled.
To adjust to this change, stop using the existing dialect registration: the
global registry will be removed soon.
1) For passes, you need to override the method:
virtual void getDependentDialects(DialectRegistry ®istry) const {}
and registery on the provided registry any dialect that this pass can produce.
Passes defined in TableGen can provide this list in the dependentDialects list
field.
2) For dialects, on construction you can register dependent dialects using the
provided MLIRContext: `context.getOrLoadDialect<DialectName>()`
This is useful if a dialect may canonicalize or have interfaces involving
another dialect.
3) For loading IR, dialect that can be in the input file must be explicitly
registered with the context. `MlirOptMain()` is taking an explicit registry for
this purpose. See how the standalone-opt.cpp example is setup:
mlir::DialectRegistry registry;
registry.insert<mlir::standalone::StandaloneDialect>();
registry.insert<mlir::StandardOpsDialect>();
Only operations from these two dialects can be in the input file. To include all
of the dialects in MLIR Core, you can populate the registry this way:
mlir::registerAllDialects(registry);
4) For `mlir-translate` callback, as well as frontend, Dialects can be loaded in
the context before emitting the IR: context.getOrLoadDialect<ToyDialect>()
Differential Revision: https://reviews.llvm.org/D85622
2020-08-19 04:01:19 +08:00
|
|
|
static thread_local bool once =
|
|
|
|
(context.getOrLoadDialect<SDBMDialect>(), true);
|
|
|
|
(void)once;
|
2019-05-20 16:44:37 +08:00
|
|
|
return &context;
|
|
|
|
}
|
|
|
|
|
2019-05-21 22:25:55 +08:00
|
|
|
static SDBMDialect *dialect() {
|
|
|
|
static thread_local SDBMDialect *d = nullptr;
|
|
|
|
if (!d) {
|
Separate the Registration from Loading dialects in the Context
This changes the behavior of constructing MLIRContext to no longer load globally
registered dialects on construction. Instead Dialects are only loaded explicitly
on demand:
- the Parser is lazily loading Dialects in the context as it encounters them
during parsing. This is the only purpose for registering dialects and not load
them in the context.
- Passes are expected to declare the dialects they will create entity from
(Operations, Attributes, or Types), and the PassManager is loading Dialects into
the Context when starting a pipeline.
This changes simplifies the configuration of the registration: a compiler only
need to load the dialect for the IR it will emit, and the optimizer is
self-contained and load the required Dialects. For example in the Toy tutorial,
the compiler only needs to load the Toy dialect in the Context, all the others
(linalg, affine, std, LLVM, ...) are automatically loaded depending on the
optimization pipeline enabled.
To adjust to this change, stop using the existing dialect registration: the
global registry will be removed soon.
1) For passes, you need to override the method:
virtual void getDependentDialects(DialectRegistry ®istry) const {}
and registery on the provided registry any dialect that this pass can produce.
Passes defined in TableGen can provide this list in the dependentDialects list
field.
2) For dialects, on construction you can register dependent dialects using the
provided MLIRContext: `context.getOrLoadDialect<DialectName>()`
This is useful if a dialect may canonicalize or have interfaces involving
another dialect.
3) For loading IR, dialect that can be in the input file must be explicitly
registered with the context. `MlirOptMain()` is taking an explicit registry for
this purpose. See how the standalone-opt.cpp example is setup:
mlir::DialectRegistry registry;
registry.insert<mlir::standalone::StandaloneDialect>();
registry.insert<mlir::StandardOpsDialect>();
Only operations from these two dialects can be in the input file. To include all
of the dialects in MLIR Core, you can populate the registry this way:
mlir::registerAllDialects(registry);
4) For `mlir-translate` callback, as well as frontend, Dialects can be loaded in
the context before emitting the IR: context.getOrLoadDialect<ToyDialect>()
Differential Revision: https://reviews.llvm.org/D85622
2020-08-19 04:01:19 +08:00
|
|
|
d = ctx()->getOrLoadDialect<SDBMDialect>();
|
2019-05-21 22:25:55 +08:00
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SDBMExpr dim(unsigned pos) { return SDBMDimExpr::get(dialect(), pos); }
|
2019-05-20 16:44:37 +08:00
|
|
|
|
2019-05-21 22:25:55 +08:00
|
|
|
static SDBMExpr symb(unsigned pos) {
|
|
|
|
return SDBMSymbolExpr::get(dialect(), pos);
|
|
|
|
}
|
2019-05-20 16:44:37 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using namespace mlir::ops_assertions;
|
|
|
|
|
|
|
|
TEST_FUNC(SDBM_SingleConstraint) {
|
|
|
|
// Build an SDBM defined by
|
|
|
|
// d0 - 3 <= 0 <=> d0 <= 3.
|
|
|
|
auto sdbm = SDBM::get(dim(0) - 3, llvm::None);
|
|
|
|
|
|
|
|
// CHECK: cst d0
|
|
|
|
// CHECK-NEXT: cst inf 3
|
|
|
|
// CHECK-NEXT: d0 inf inf
|
|
|
|
sdbm.print(llvm::outs());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_FUNC(SDBM_Equality) {
|
|
|
|
// Build an SDBM defined by
|
|
|
|
//
|
|
|
|
// d0 - d1 - 3 = 0
|
|
|
|
// <=> {d0 - d1 - 3 <= 0 and d0 - d1 - 3 >= 0}
|
|
|
|
// <=> {d0 - d1 <= 3 and d1 - d0 <= -3}.
|
|
|
|
auto sdbm = SDBM::get(llvm::None, dim(0) - dim(1) - 3);
|
|
|
|
|
|
|
|
// CHECK: cst d0 d1
|
|
|
|
// CHECK-NEXT: cst inf inf inf
|
|
|
|
// CHECK-NEXT: d0 inf inf -3
|
|
|
|
// CHECK-NEXT: d1 inf 3 inf
|
|
|
|
sdbm.print(llvm::outs());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_FUNC(SDBM_TrivialSimplification) {
|
|
|
|
// Build an SDBM defined by
|
|
|
|
//
|
|
|
|
// d0 - 3 <= 0 <=> d0 <= 3
|
|
|
|
// d0 - 5 <= 0 <=> d0 <= 5
|
|
|
|
//
|
2019-10-21 00:44:06 +08:00
|
|
|
// which should get simplified on construction to only the former.
|
2019-05-20 16:44:37 +08:00
|
|
|
auto sdbm = SDBM::get({dim(0) - 3, dim(0) - 5}, llvm::None);
|
|
|
|
|
|
|
|
// CHECK: cst d0
|
|
|
|
// CHECK-NEXT: cst inf 3
|
|
|
|
// CHECK-NEXT: d0 inf inf
|
|
|
|
sdbm.print(llvm::outs());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_FUNC(SDBM_StripeInducedIneqs) {
|
|
|
|
// Build an SDBM defined by d1 = d0 # 3, which induces the constraints
|
|
|
|
//
|
|
|
|
// d1 - d0 <= 0
|
|
|
|
// d0 - d1 <= 3 - 1 = 2
|
|
|
|
auto sdbm = SDBM::get(llvm::None, dim(1) - stripe(dim(0), 3));
|
|
|
|
|
|
|
|
// CHECK: cst d0 d1
|
|
|
|
// CHECK-NEXT: cst inf inf inf
|
2019-06-04 01:00:16 +08:00
|
|
|
// CHECK-NEXT: d0 inf inf 0
|
|
|
|
// CHECK-NEXT: d1 inf 2 0
|
2019-05-20 16:44:37 +08:00
|
|
|
// CHECK-NEXT: d1 = d0 # 3
|
|
|
|
sdbm.print(llvm::outs());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_FUNC(SDBM_StripeTemporaries) {
|
|
|
|
// Build an SDBM defined by d0 # 3 <= 0, which creates a temporary
|
|
|
|
// t0 = d0 # 3 leading to a constraint t0 <= 0 and the stripe-induced
|
|
|
|
// constraints
|
|
|
|
//
|
|
|
|
// t0 - d0 <= 0
|
|
|
|
// d0 - t0 <= 3 - 1 = 2
|
|
|
|
auto sdbm = SDBM::get(stripe(dim(0), 3), llvm::None);
|
|
|
|
|
|
|
|
// CHECK: cst d0 t0
|
|
|
|
// CHECK-NEXT: cst inf inf 0
|
2019-06-04 01:00:16 +08:00
|
|
|
// CHECK-NEXT: d0 inf inf 0
|
|
|
|
// CHECK-NEXT: t0 inf 2 inf
|
2019-05-20 16:44:37 +08:00
|
|
|
// CHECK-NEXT: t0 = d0 # 3
|
|
|
|
sdbm.print(llvm::outs());
|
|
|
|
}
|
|
|
|
|
2019-06-04 01:00:16 +08:00
|
|
|
TEST_FUNC(SDBM_ElideInducedInequalities) {
|
|
|
|
// Build an SDBM defined by a single stripe equality d0 = s0 # 3 and make sure
|
|
|
|
// the induced inequalities are not present after converting the SDBM back
|
|
|
|
// into lists of expressions.
|
|
|
|
auto sdbm = SDBM::get(llvm::None, {dim(0) - stripe(symb(0), 3)});
|
|
|
|
|
|
|
|
SmallVector<SDBMExpr, 4> eqs, ineqs;
|
|
|
|
sdbm.getSDBMExpressions(dialect(), ineqs, eqs);
|
|
|
|
// CHECK-EMPTY:
|
|
|
|
for (auto ineq : ineqs)
|
|
|
|
ineq.print(llvm::outs() << '\n');
|
|
|
|
llvm::outs() << "\n";
|
|
|
|
|
|
|
|
// CHECK: d0 - s0 # 3
|
|
|
|
// CHECK-EMPTY:
|
|
|
|
for (auto eq : eqs)
|
|
|
|
eq.print(llvm::outs() << '\n');
|
|
|
|
llvm::outs() << "\n\n";
|
|
|
|
}
|
|
|
|
|
2019-05-20 16:44:37 +08:00
|
|
|
TEST_FUNC(SDBM_StripeTightening) {
|
|
|
|
// Build an SDBM defined by
|
|
|
|
//
|
|
|
|
// d0 = s0 # 3 # 5
|
|
|
|
// s0 # 3 # 5 - d1 + 42 = 0
|
2019-06-04 01:00:16 +08:00
|
|
|
// s0 # 3 - d0 <= 2
|
2019-05-20 16:44:37 +08:00
|
|
|
//
|
|
|
|
// where the last inequality is tighter than that induced by the first stripe
|
2019-06-04 01:00:16 +08:00
|
|
|
// equality (s0 # 3 - d0 <= 5 - 1 = 4). Check that the conversion from SDBM
|
2019-05-20 16:44:37 +08:00
|
|
|
// back to the lists of constraints conserves both the stripe equality and the
|
|
|
|
// tighter inequality.
|
|
|
|
auto s = stripe(stripe(symb(0), 3), 5);
|
2019-06-04 01:00:16 +08:00
|
|
|
auto tight = stripe(symb(0), 3) - dim(0) - 2;
|
2019-05-20 16:44:37 +08:00
|
|
|
auto sdbm = SDBM::get({tight}, {s - dim(0), s - dim(1) + 42});
|
|
|
|
|
|
|
|
SmallVector<SDBMExpr, 4> eqs, ineqs;
|
2019-05-21 22:25:55 +08:00
|
|
|
sdbm.getSDBMExpressions(dialect(), ineqs, eqs);
|
2019-09-16 23:15:25 +08:00
|
|
|
// CHECK: s0 # 3 + -2 - d0
|
2019-06-04 01:00:16 +08:00
|
|
|
// CHECK-EMPTY:
|
2019-05-20 16:44:37 +08:00
|
|
|
for (auto ineq : ineqs)
|
2019-06-04 01:00:16 +08:00
|
|
|
ineq.print(llvm::outs() << '\n');
|
|
|
|
llvm::outs() << "\n";
|
|
|
|
|
2019-09-16 23:15:25 +08:00
|
|
|
// CHECK-DAG: d1 + -42 - d0
|
2019-06-04 01:00:16 +08:00
|
|
|
// CHECK-DAG: d0 - s0 # 3 # 5
|
2019-05-20 16:44:37 +08:00
|
|
|
for (auto eq : eqs)
|
2019-06-04 01:00:16 +08:00
|
|
|
eq.print(llvm::outs() << '\n');
|
|
|
|
llvm::outs() << "\n\n";
|
2019-05-20 16:44:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_FUNC(SDBM_StripeTransitive) {
|
|
|
|
// Build an SDBM defined by
|
|
|
|
//
|
|
|
|
// d0 = d1 # 3
|
|
|
|
// d0 = d2 # 7
|
|
|
|
//
|
|
|
|
// where the same dimension is declared equal to two stripe expressions over
|
|
|
|
// different variables. This is practically handled by introducing a
|
|
|
|
// temporary variable for the second stripe expression and adding an equality
|
|
|
|
// constraint between this variable and the original dimension variable.
|
|
|
|
auto sdbm = SDBM::get(
|
|
|
|
llvm::None, {stripe(dim(1), 3) - dim(0), stripe(dim(2), 7) - dim(0)});
|
|
|
|
|
|
|
|
// CHECK: cst d0 d1 d2 t0
|
|
|
|
// CHECK-NEXT: cst inf inf inf inf inf
|
2019-06-04 01:00:16 +08:00
|
|
|
// CHECK-NEXT: d0 inf 0 2 inf 0
|
|
|
|
// CHECK-NEXT: d1 inf 0 inf inf inf
|
|
|
|
// CHECK-NEXT: d2 inf inf inf inf 0
|
|
|
|
// CHECK-NEXT: t0 inf 0 inf 6 inf
|
2019-05-20 16:44:37 +08:00
|
|
|
// CHECK-NEXT: t0 = d2 # 7
|
|
|
|
// CHECK-NEXT: d0 = d1 # 3
|
|
|
|
sdbm.print(llvm::outs());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
RUN_TESTS();
|
|
|
|
return 0;
|
|
|
|
}
|