summaryrefslogtreecommitdiff
path: root/willow/unittest/IR/VerifierTest.cpp
blob: 1b71eb8e4d1b7db68ed2a4eaa1fd8a537767ea79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#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;
  DiagnosticEngine eng;

  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)));
  }
}