Loading [MathJax]/extensions/tex2jax.js
CIRCT 21.0.0git
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tester.h
Go to the documentation of this file.
1//===- Tester.h -------------------------------------------------*- 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 the Tester class used in the CIRCT reduce tool.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef CIRCT_REDUCE_TESTER_H
14#define CIRCT_REDUCE_TESTER_H
15
16#include "circt/Support/LLVM.h"
17#include "mlir/IR/BuiltinOps.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/Support/Error.h"
20#include "llvm/Support/FileSystem.h"
21#include "llvm/Support/Program.h"
22
23namespace llvm {
24class ToolOutputFile;
25} // namespace llvm
26
27namespace circt {
28
29class TestCase;
30
31/// A testing environment for reduction attempts.
32///
33/// This class tracks the program used to check reduction attempts for
34/// interestingness and additional arguments to pass to that tool. Use `get()`
35/// to obtain a new test case that can be queried for information on an
36/// individual MLIR module.
37class Tester {
38public:
39 Tester(llvm::StringRef testScript, llvm::ArrayRef<std::string> testScriptArgs,
40 bool testMustFail, bool emitBytecode);
41
42 /// Runs the interestingness testing script on a MLIR test case file. Returns
43 /// true if the interesting behavior is present in the test case or false
44 /// otherwise.
45 std::pair<bool, size_t> isInteresting(mlir::ModuleOp module) const;
46
47 /// Return whether the file in the given path is interesting.
48 bool isInteresting(llvm::StringRef testCase) const;
49
50 /// Create a new test case for the given `module`.
51 TestCase get(mlir::ModuleOp module) const;
52
53 /// Create a new test case for the given file already on disk.
54 TestCase get(llvm::Twine filepath) const;
55
56 /// If true, use MLIR bytecode on-disk. Otherwise, use MLIR text.
57 const bool emitBytecode;
58
59private:
60 /// The binary to execute in order to check a reduction attempt for
61 /// interestingness.
62 llvm::StringRef testScript;
63
64 /// Additional arguments to pass to `testScript`.
65 llvm::ArrayRef<std::string> testScriptArgs;
66
67 /// Consider the testcase to be interesting if it fails rather than on exit
68 /// code 0.
70};
71
72/// A single test case to be run by a tester.
73///
74/// This is a helper object that wraps a `ModuleOp` and can be used to query
75/// initial information about the test, such as validity of the module and size
76/// on disk, before the test is actually executed.
77class TestCase {
78public:
79 /// Create a test case with an MLIR module that will be written to a temporary
80 /// file on disk. The `TestCase` will clean up the temporary file after use.
81 TestCase(const Tester &tester, mlir::ModuleOp module)
82 : tester(tester), module(module) {}
83
84 /// Create a test case for an already-prepared file on disk. The caller
85 /// remains responsible for cleaning up the file on disk.
86 TestCase(const Tester &tester, llvm::Twine filepath) : tester(tester) {
87 filepath.toVector(this->filepath);
88 }
89
90 /// Check whether the MLIR module is valid. Actual validation is only
91 /// performed on the first call; subsequent calls return the cached result.
92 bool isValid();
93
94 /// Determine the path to the MLIR module on disk. Actual writing to disk is
95 /// only performed on the first call; subsequent calls return the cached
96 /// result.
97 llvm::StringRef getFilepath();
98
99 /// Determine the size of the MLIR module on disk. Actual writing to disk is
100 /// only performed on the first call; subsequent calls return the cached
101 /// result.
102 size_t getSize();
103
104 /// Run the tester on the MLIR module and return whether it is deemed
105 /// interesting. Actual testing is only performed on the first call;
106 /// subsequent calls return the cached result.
107 bool isInteresting();
108
109private:
110 friend class Tester;
111
112 /// Ensure `filepath` and `size` are populated, and that the test case is in a
113 /// file on disk.
114 void ensureFileOnDisk();
115
116 /// The tester that is used to run this test case.
118 /// The module to be tested.
119 mlir::ModuleOp module;
120 /// The path on disk where the test case is located.
121 llvm::SmallString<32> filepath;
122
123 /// In case this test case has created a temporary file on disk, this is the
124 /// `ToolOutputFile` that did the writing. Keeping this class around ensures
125 /// that the file will be cleaned up properly afterwards. This field remains
126 /// null if the user already has provided a filepath in the constructor.
127 std::unique_ptr<llvm::ToolOutputFile> file;
128
129 /// Whether the MLIR module validation has run, and its result.
130 std::optional<bool> valid;
131 /// Whether the size of the test case on disk has already been determined, and
132 /// if yes, that size.
133 std::optional<size_t> size;
134 /// Whether the tester has run on this test case, and its result.
135 std::optional<bool> interesting;
136};
137
138} // namespace circt
139
140#endif // CIRCT_REDUCE_TESTER_H
A single test case to be run by a tester.
Definition Tester.h:77
std::optional< bool > interesting
Whether the tester has run on this test case, and its result.
Definition Tester.h:135
TestCase(const Tester &tester, llvm::Twine filepath)
Create a test case for an already-prepared file on disk.
Definition Tester.h:86
TestCase(const Tester &tester, mlir::ModuleOp module)
Create a test case with an MLIR module that will be written to a temporary file on disk.
Definition Tester.h:81
mlir::ModuleOp llvm::SmallString< 32 > filepath
The module to be tested.
Definition Tester.h:121
size_t getSize()
Determine the size of the MLIR module on disk.
Definition Tester.cpp:99
bool isValid()
Check whether the MLIR module is valid.
Definition Tester.cpp:79
std::optional< size_t > size
Whether the size of the test case on disk has already been determined, and if yes,...
Definition Tester.h:133
void ensureFileOnDisk()
Ensure filepath and size are populated, and that the test case is in a file on disk.
Definition Tester.cpp:120
std::optional< bool > valid
Whether the MLIR module validation has run, and its result.
Definition Tester.h:130
bool isInteresting()
Run the tester on the MLIR module and return whether it is deemed interesting.
Definition Tester.cpp:109
const Tester & tester
The tester that is used to run this test case.
Definition Tester.h:117
llvm::StringRef getFilepath()
Determine the path to the MLIR module on disk.
Definition Tester.cpp:90
std::unique_ptr< llvm::ToolOutputFile > file
In case this test case has created a temporary file on disk, this is the ToolOutputFile that did the ...
Definition Tester.h:127
A testing environment for reduction attempts.
Definition Tester.h:37
llvm::StringRef testScript
The binary to execute in order to check a reduction attempt for interestingness.
Definition Tester.h:62
const bool emitBytecode
If true, use MLIR bytecode on-disk. Otherwise, use MLIR text.
Definition Tester.h:57
bool isInteresting(llvm::StringRef testCase) const
Return whether the file in the given path is interesting.
std::pair< bool, size_t > isInteresting(mlir::ModuleOp module) const
Runs the interestingness testing script on a MLIR test case file.
bool testMustFail
Consider the testcase to be interesting if it fails rather than on exit code 0.
Definition Tester.h:69
TestCase get(mlir::ModuleOp module) const
Create a new test case for the given module.
Definition Tester.cpp:64
llvm::ArrayRef< std::string > testScriptArgs
Additional arguments to pass to testScript.
Definition Tester.h:65
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.