diff options
Diffstat (limited to 'willow/unittest')
| -rw-r--r-- | willow/unittest/BUILD.bazel | 2 | ||||
| -rw-r--r-- | willow/unittest/IR/BUILD.bazel (renamed from willow/unittest/ir/BUILD.bazel) | 0 | ||||
| -rw-r--r-- | willow/unittest/IR/VerifierTest.cpp | 82 | ||||
| -rw-r--r-- | willow/unittest/ir/VerifierTest.cpp | 51 |
4 files changed, 83 insertions, 52 deletions
diff --git a/willow/unittest/BUILD.bazel b/willow/unittest/BUILD.bazel index 8f1c55d..bfd0430 100644 --- a/willow/unittest/BUILD.bazel +++ b/willow/unittest/BUILD.bazel @@ -1,6 +1,6 @@ test_suite( name = "unittest", tests = [ - "//willow/unittest/ir:ir_tests", + "//willow/unittest/IR:ir_tests", ] ) diff --git a/willow/unittest/ir/BUILD.bazel b/willow/unittest/IR/BUILD.bazel index b41dfcd..b41dfcd 100644 --- a/willow/unittest/ir/BUILD.bazel +++ b/willow/unittest/IR/BUILD.bazel diff --git a/willow/unittest/IR/VerifierTest.cpp b/willow/unittest/IR/VerifierTest.cpp new file mode 100644 index 0000000..efe34db --- /dev/null +++ b/willow/unittest/IR/VerifierTest.cpp @@ -0,0 +1,82 @@ +#include <catch2/catch_test_macros.hpp> + +#include <willow/IR/Context.h> +#include <willow/IR/Diagnostic.h> +#include <willow/IR/DiagnosticEngine.h> +#include <willow/IR/Function.h> +#include <willow/IR/IRBuilder.h> +#include <willow/IR/Module.h> +#include <willow/IR/Verifier.h> + +using namespace willow; + +TEST_CASE("valid modules", "[verifier]") { + WillowContext ctx; + std::vector<Diagnostic> diags; + DiagnosticEngine eng([&](Diagnostic d) { diags.push_back(std::move(d)); }); + + auto &m = *ctx.addModule("test"); + SECTION("empty module") { + REQUIRE(succeeded(verifyModule(ctx, m, eng))); + REQUIRE(diags.empty()); + } +} + +TEST_CASE("valid function", "[verifier]") { + WillowContext ctx; + std::vector<Diagnostic> diags; + DiagnosticEngine eng([&](Diagnostic d) { diags.push_back(std::move(d)); }); + + auto &m = *ctx.addModule("test"); + + Type fty = ctx.types().FunctionType(ctx.types().VoidType(), {}); + auto &fn = *m.emplaceFunction("fn", &m, fty); + + REQUIRE(succeeded(verifyFunction(ctx, fn, eng))); + REQUIRE(diags.empty()); +} + +TEST_CASE("invalid basic block", "[verifier]") { + WillowContext ctx; + std::vector<Diagnostic> diags; + DiagnosticEngine eng([&](Diagnostic d) { diags.push_back(std::move(d)); }); + + Type i64Ty = ctx.types().IntType(64); + Type voidTy = ctx.types().VoidType(); + auto *one = ctx.constants().getInt(i64Ty, 1); + + auto &m = *ctx.addModule("test"); + Type fty = ctx.types().FunctionType(ctx.types().VoidType(), {}); + Type ifty = ctx.types().FunctionType(i64Ty, {}); + auto &fn = *m.emplaceFunction("fn", &m, fty); + auto &fn2 = *m.emplaceFunction("fn2", &m, ifty); + auto *bb = fn.addBlock( + std::make_unique<BasicBlock>(&fn, ctx.types().BasicBlockType())); + IRBuilder builder{ctx, *bb, bb->end()}; + + SECTION("Empty basic block") { + REQUIRE(bb->empty()); + REQUIRE(failed(verifyBasicBlock(ctx, *bb, eng))); + } + + SECTION("Basic block with no terminator") { + builder.BuildAdd(i64Ty, one, one); + REQUIRE(failed(verifyBasicBlock(ctx, *bb, eng))); + } + + SECTION("Teminator must be the last instruction in a basic block") { + builder.BuildCall(i64Ty, &fn2); + builder.BuildAdd(i64Ty, one, one); + REQUIRE(failed(verifyBasicBlock(ctx, *bb, eng))); + } + + SECTION("Basic block with invalid instruction") { + auto *bb2 = fn.addBlock( + std::make_unique<BasicBlock>(&fn, ctx.types().BasicBlockType())); + builder.SetInsertPoint(bb2->end()); + builder.BuildAdd(voidTy, one, one); + builder.BuildRet(); + + REQUIRE(failed(verifyBasicBlock(ctx, *bb, eng))); + } +} diff --git a/willow/unittest/ir/VerifierTest.cpp b/willow/unittest/ir/VerifierTest.cpp deleted file mode 100644 index 959d72a..0000000 --- a/willow/unittest/ir/VerifierTest.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include <catch2/catch_test_macros.hpp> - -#include <willow/IR/Context.h> -#include <willow/IR/Module.h> -#include <willow/IR/Verifier.h> -#include <willow/IR/Diagnostic.h> -#include <willow/IR/DiagnosticEngine.h> -#include <willow/IR/Function.h> - -using namespace willow; - -TEST_CASE("valid modules", "[verifier]") { - WillowContext ctx; - std::vector<Diagnostic> diags; - DiagnosticEngine eng( - [&](Diagnostic d) { diags.push_back(std::move(d)); }); - - auto &m = *ctx.addModule("test"); - SECTION("empty module") { - REQUIRE(succeeded(verifyModule(ctx, m, eng))); - REQUIRE(diags.empty()); - } -} - -TEST_CASE("valid function", "[verifier]") { - WillowContext ctx; - std::vector<Diagnostic> diags; - DiagnosticEngine eng( - [&](Diagnostic d) { diags.push_back(std::move(d)); }); - - auto &m = *ctx.addModule("test"); - - Type fty = ctx.types().FunctionType(ctx.types().VoidType(), {}); - auto &fn = *m.emplaceFunction("fn", &m, fty); - - REQUIRE(succeeded(verifyFunction(ctx, fn, eng))); - REQUIRE(diags.empty()); -} - -TEST_CASE("invalid basic block", "[verifier]") { - WillowContext ctx; - std::vector<Diagnostic> diags; - DiagnosticEngine eng( - [&](Diagnostic d) { diags.push_back(std::move(d)); }); - - auto &m = *ctx.addModule("test"); - - Type fty = ctx.types().FunctionType(ctx.types().VoidType(), {}); - auto &fn = *m.emplaceFunction("fn", &m, fty); - // TODO -} |