diff --git a/docs/Dialects/RTG.md b/docs/Dialects/RTG.md index b217daeb5ac1..7348b093b6a9 100644 --- a/docs/Dialects/RTG.md +++ b/docs/Dialects/RTG.md @@ -273,3 +273,7 @@ companion dialect to define any backends. ## Types [include "Dialects/RTGTypes.md"] + +## Passes + +[include "Dialects/RTGPasses.md"] diff --git a/include/circt/Dialect/RTG/CMakeLists.txt b/include/circt/Dialect/RTG/CMakeLists.txt index f33061b2d87c..9f57627c321f 100644 --- a/include/circt/Dialect/RTG/CMakeLists.txt +++ b/include/circt/Dialect/RTG/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(IR) +add_subdirectory(Transforms) diff --git a/include/circt/Dialect/RTG/Transforms/CMakeLists.txt b/include/circt/Dialect/RTG/Transforms/CMakeLists.txt new file mode 100644 index 000000000000..06f8b8112747 --- /dev/null +++ b/include/circt/Dialect/RTG/Transforms/CMakeLists.txt @@ -0,0 +1,6 @@ +set(LLVM_TARGET_DEFINITIONS RTGPasses.td) +mlir_tablegen(RTGPasses.h.inc -gen-pass-decls) +add_public_tablegen_target(CIRCTRTGTransformsIncGen) + +# Generate Pass documentation. +add_circt_doc(RTGPasses RTGPasses -gen-pass-doc) diff --git a/include/circt/Dialect/RTG/Transforms/RTGPasses.h b/include/circt/Dialect/RTG/Transforms/RTGPasses.h new file mode 100644 index 000000000000..a2a31021db0b --- /dev/null +++ b/include/circt/Dialect/RTG/Transforms/RTGPasses.h @@ -0,0 +1,31 @@ +//===- RTGPasses.h - RTG pass entry points ----------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This header file defines prototypes that expose pass constructors. +// +//===----------------------------------------------------------------------===// + +#ifndef CIRCT_DIALECT_RTG_TRANSFORMS_RTGPASSES_H +#define CIRCT_DIALECT_RTG_TRANSFORMS_RTGPASSES_H + +#include "mlir/Pass/Pass.h" +#include + +namespace circt { +namespace rtg { + +/// Generate the code for registering passes. +#define GEN_PASS_REGISTRATION +#define GEN_PASS_DECL +#include "circt/Dialect/RTG/Transforms/RTGPasses.h.inc" +#undef GEN_PASS_REGISTRATION + +} // namespace rtg +} // namespace circt + +#endif // CIRCT_DIALECT_RTG_TRANSFORMS_RTGPASSES_H diff --git a/include/circt/Dialect/RTG/Transforms/RTGPasses.td b/include/circt/Dialect/RTG/Transforms/RTGPasses.td new file mode 100644 index 000000000000..3e13a6d6ce54 --- /dev/null +++ b/include/circt/Dialect/RTG/Transforms/RTGPasses.td @@ -0,0 +1,37 @@ +//===-- RTGPasses.td - RTG pass definition file ------------*- tablegen -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file defines the passes that operate on the RTG dialect. +// +//===----------------------------------------------------------------------===// + +#ifndef CIRCT_DIALECT_RTG_TRANSFORMS_RTGPASSES_TD +#define CIRCT_DIALECT_RTG_TRANSFORMS_RTGPASSES_TD + +include "mlir/Pass/PassBase.td" + +def ElaborationPass : Pass<"rtg-elaborate", "mlir::ModuleOp"> { + let summary = "elaborate the randomization parts"; + let description = [{ + This pass interprets most RTG operations to perform the represented + randomization and in the process get rid of those operations. This means, + after this pass the IR does not contain any random constructs within tests + anymore. + }]; + + let options = [ + Option<"seed", "seed", "unsigned", /*default=*/"", + "The seed for any RNG constructs used in the pass.">, + Option<"debugMode", "debug", "bool", /*default=*/"false", + "When in debug mode the pass queries the values that would otherwise be " + "provided by the RNG from an attribute attached to the operation called " + "'rtg.elaboration'.">, + ]; +} + +#endif // CIRCT_DIALECT_RTG_TRANSFORMS_RTGPASSES_TD diff --git a/include/circt/InitAllPasses.h b/include/circt/InitAllPasses.h index 48ed141491c8..9f3c1d7f695e 100644 --- a/include/circt/InitAllPasses.h +++ b/include/circt/InitAllPasses.h @@ -32,6 +32,7 @@ #include "circt/Dialect/Moore/MoorePasses.h" #include "circt/Dialect/OM/OMPasses.h" #include "circt/Dialect/Pipeline/PipelinePasses.h" +#include "circt/Dialect/RTG/Transforms/RTGPasses.h" #include "circt/Dialect/SSP/SSPPasses.h" #include "circt/Dialect/SV/SVPasses.h" #include "circt/Dialect/Seq/SeqPasses.h" @@ -73,6 +74,7 @@ inline void registerAllPasses() { sv::registerPasses(); handshake::registerPasses(); kanagawa::registerPasses(); + rtg::registerPasses(); hw::registerPasses(); pipeline::registerPasses(); sim::registerPasses(); diff --git a/lib/Dialect/RTG/CMakeLists.txt b/lib/Dialect/RTG/CMakeLists.txt index f33061b2d87c..9f57627c321f 100644 --- a/lib/Dialect/RTG/CMakeLists.txt +++ b/lib/Dialect/RTG/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(IR) +add_subdirectory(Transforms) diff --git a/lib/Dialect/RTG/Transforms/CMakeLists.txt b/lib/Dialect/RTG/Transforms/CMakeLists.txt new file mode 100644 index 000000000000..cd363a383510 --- /dev/null +++ b/lib/Dialect/RTG/Transforms/CMakeLists.txt @@ -0,0 +1,15 @@ +add_circt_dialect_library(CIRCTRTGTransforms + ElaborationPass.cpp + + DEPENDS + CIRCTRTGTransformsIncGen + + LINK_COMPONENTS + Support + + LINK_LIBS PRIVATE + CIRCTRTGDialect + MLIRIR + MLIRPass +) + diff --git a/lib/Dialect/RTG/Transforms/ElaborationPass.cpp b/lib/Dialect/RTG/Transforms/ElaborationPass.cpp new file mode 100644 index 000000000000..44e9b39f2898 --- /dev/null +++ b/lib/Dialect/RTG/Transforms/ElaborationPass.cpp @@ -0,0 +1,520 @@ +//===- ElaborationPass.cpp - RTG ElaborationPass implementation -----------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This pass elaborates the random parts of the RTG dialect. +// It performs randomization top-down, i.e., random constructs in a sequence +// that is invoked multiple times can yield different randomization results +// for each invokation. +// +//===----------------------------------------------------------------------===// + +#include "circt/Dialect/RTG/IR/RTGOps.h" +#include "circt/Dialect/RTG/IR/RTGVisitors.h" +#include "circt/Dialect/RTG/Transforms/RTGPasses.h" +#include "mlir/IR/IRMapping.h" +#include "mlir/IR/PatternMatch.h" +#include +#include + +namespace circt { +namespace rtg { +#define GEN_PASS_DEF_ELABORATIONPASS +#include "circt/Dialect/RTG/Transforms/RTGPasses.h.inc" +} // namespace rtg +} // namespace circt + +using namespace mlir; +using namespace circt; +using namespace circt::rtg; + +#define DEBUG_TYPE "rtg-elaboration" + +namespace { + +//===----------------------------------------------------------------------===// +// Elaborator Values +//===----------------------------------------------------------------------===// + +/// The base class for elaborated values. Using the base class directly +/// represents an opaque value, i.e., an SSA value which we cannot further +/// interpret. +/// Derived classes will also hold the opaque value such that we always have an +/// SSA value at hand that we can use as a replacement for the concrete +/// interpreted value when needed (alternatively we could materialize new IR, +/// but that's more expensive). +/// The derived classes are supposed to +/// * add fields necessary to hold the concrete value +/// * override the virtual methods to compute equivalence based on the +/// interpreted values +/// * implement LLVM's RTTI mechanism +class ElaboratorValue { +public: + /// Creates an opaque value. + ElaboratorValue(Value value) : ElaboratorValue(value, true) { + assert(value && "null values not allowed"); + } + + virtual ~ElaboratorValue() {} + + Type getType() const { return value.getType(); } + Value getOpaqueValue() const { return value; } + bool isOpaqueValue() const { return isOpaque; } + virtual bool containsOpaqueValue() const { return isOpaque; } + + virtual llvm::hash_code getHashValue() const { + return llvm::hash_combine(value, isOpaque); + } + + virtual bool isEqual(const ElaboratorValue &other) const { + return isOpaque == other.isOpaque && value == other.value; + } + +protected: + ElaboratorValue(Value value, bool isOpaque) + : isOpaque(isOpaque), value(value) {} + +private: + const bool isOpaque; + const Value value; +}; + +/// Holds an evaluated value of a `SetType`'d value. +class SetValue : public ElaboratorValue { +public: + SetValue(Value value, SmallVector &&set, + bool debug = false) + : ElaboratorValue(value, false), originalSize(set.size()), debug(debug), + set(std::move(set)) { + assert(isa(value.getType())); + + if (debug) + for (auto [i, val] : llvm::enumerate(this->set)) + debugMap[i] = val; + + // Make sure the vector is sorted and has no duplicates. + llvm::sort(this->set); + this->set.erase(llvm::unique(this->set), this->set.end()); + } + + // Implement LLVMs RTTI + static bool classof(const ElaboratorValue *val) { + return !val->isOpaqueValue() && SetType::classof(val->getType()); + } + + bool containsOpaqueValue() const override { + return llvm::any_of(set, [](auto el) { return el->containsOpaqueValue(); }); + } + + llvm::hash_code getHashValue() const override { + return llvm::hash_combine_range(set.begin(), set.end()); + } + + bool isEqual(const ElaboratorValue &other) const override { + auto *otherSet = dyn_cast(&other); + if (!otherSet) + return false; + return set == otherSet->set; + } + + ArrayRef getAsArrayRef() const { return set; } + + ElaboratorValue *getAtIndexForDebug(size_t idx) const { + assert(debug && "must have been constructed in debug mode"); + return debugMap.lookup(idx); + } + + const size_t originalSize; + +private: + // Whether this set was constructed in debug mode. + const bool debug; + + // We currently use a sorted vector to represent sets. Note that it is sorted + // by the pointer value and thus non-deterministic. + // We probably want to do some profiling in the future to see if a DenseSet or + // other representation is better suited. + SmallVector set; + + // A map to guarantee deterministic behavior when the 'rtg.elaboration' + // attribute is used in debug mode. + DenseMap debugMap; +}; + +/// Holds an evaluated value of a `SequenceType`'d value. +class SequenceClosureValue : public ElaboratorValue { +public: + SequenceClosureValue(Value value, StringAttr sequence, + ArrayRef args) + : ElaboratorValue(value, false), sequence(sequence), args(args) { + assert(isa(value.getType())); + } + + // Implement LLVMs RTTI + static bool classof(const ElaboratorValue *val) { + return !val->isOpaqueValue() && SequenceType::classof(val->getType()); + } + + bool containsOpaqueValue() const override { + return llvm::any_of(args, + [](auto el) { return el->containsOpaqueValue(); }); + } + + llvm::hash_code getHashValue() const override { + return llvm::hash_combine( + sequence, llvm::hash_combine_range(args.begin(), args.end())); + } + + bool isEqual(const ElaboratorValue &other) const override { + auto *seq = dyn_cast(&other); + if (!seq) + return false; + + return sequence == seq->sequence && args == seq->args; + } + + StringAttr getSequence() const { return sequence; } + ArrayRef getArgs() const { return args; } + +private: + StringAttr sequence; + SmallVector args; +}; + +//===----------------------------------------------------------------------===// +// Hash Map Helpers +//===----------------------------------------------------------------------===// + +// NOLINTNEXTLINE(readability-identifier-naming) +inline llvm::hash_code hash_value(const ElaboratorValue &val) { + return val.getHashValue(); +} + +struct InternMapInfo : public DenseMapInfo { + static unsigned getHashValue(const ElaboratorValue *value) { + auto *tk = getTombstoneKey(); + auto *ek = getEmptyKey(); + if (value == tk || value == ek) + return DenseMapInfo::getHashValue(value); + + return hash_value(*value); + } + + static bool isEqual(const ElaboratorValue *lhs, const ElaboratorValue *rhs) { + if (lhs == rhs) + return true; + + auto *tk = getTombstoneKey(); + auto *ek = getEmptyKey(); + if (lhs == tk || rhs == tk || lhs == ek || rhs == ek) + return false; + + return lhs->isEqual(*rhs); + } +}; + +//===----------------------------------------------------------------------===// +// Main Elaborator Implementation +//===----------------------------------------------------------------------===// + +/// Used to signal to the elaboration driver whether the operation should be +/// removed. +enum class DeletionKind { Keep, Delete }; + +/// Interprets the IR to perform and lower the represented randomizations. +class Elaborator : public RTGOpVisitor, + function_ref> { +public: + using RTGOpVisitor, + function_ref>::visitOp; + + Elaborator(SymbolTable &table, const ElaborationPassOptions &options) + : options(options), rng(std::mt19937(options.seed)), symTable(table) {} + + /// Helper to perform internalization and keep track of interpreted value for + /// the given SSA value. + template + void internalizeResult(Value val, Args &&...args) { + auto ptr = std::make_unique(val, std::forward(args)...); + auto *e = ptr.get(); + auto [iter, _] = interned.insert({e, std::move(ptr)}); + state[val] = iter->second.get(); + } + + /// Print a nice error message for operations we don't support yet. + FailureOr + visitUnhandledOp(Operation *op, + function_ref addToWorklist) { + return op->emitError("elaboration not supported"); + } + + FailureOr + visitExternalOp(Operation *op, + function_ref addToWorklist) { + // Treat values defined by external ops as opaque, non-elaborated values. + for (auto res : op->getResults()) + internalizeResult(res); + + return DeletionKind::Keep; + } + + FailureOr + visitOp(SequenceClosureOp op, function_ref addToWorklist) { + SmallVector args; + args.reserve(op.getArgs().size()); + for (auto arg : op.getArgs()) + args.push_back(state.at(arg)); + + internalizeResult(op.getResult(), + op.getSequenceAttr(), args); + + return DeletionKind::Delete; + } + + FailureOr + visitOp(InvokeSequenceOp op, function_ref addToWorklist) { + auto *sequenceClosure = + cast(state.at(op.getSequence())); + + IRRewriter rewriter(op); + auto sequence = symTable.lookupNearestSymbolFrom( + op->getParentOfType(), sequenceClosure->getSequence()); + auto *clone = sequence->clone(); + SmallVector args; + args.reserve(sequenceClosure->getArgs().size()); + for (auto &arg : sequenceClosure->getArgs()) { + Value val = arg->getOpaqueValue(); + // Note: If the value is defined inside the same block it must be before + // this op as we would already have a dominance violation to start with. + if (val.getParentBlock() != op->getBlock()) + return op.emitError("closure argument defined outside this block"); + + args.push_back(val); + } + + for (auto &op : clone->getRegion(0).front()) + addToWorklist(&op); + + rewriter.inlineBlockBefore(&clone->getRegion(0).front(), op, args); + clone->erase(); + return DeletionKind::Delete; + } + + FailureOr + visitOp(SetCreateOp op, function_ref addToWorklist) { + SmallVector set; + for (auto val : op.getElements()) { + auto *interpValue = state.at(val); + if (interpValue->containsOpaqueValue()) + return op->emitError("cannot create a set of opaque values because " + "they cannot be reliably uniqued"); + set.emplace_back(interpValue); + } + + internalizeResult(op.getSet(), std::move(set), options.debugMode); + return DeletionKind::Delete; + } + + FailureOr + visitOp(SetSelectRandomOp op, function_ref addToWorklist) { + auto *set = cast(state.at(op.getSet())); + + ElaboratorValue *selected; + if (options.debugMode) { + auto intAttr = op->getAttrOfType("rtg.elaboration"); + if (set->originalSize != set->getAsArrayRef().size()) + op->emitWarning("set contained ") + << (set->originalSize - set->getAsArrayRef().size()) + << " duplicate value(s), the value at index " << intAttr.getInt() + << " might not be the intended one"; + selected = set->getAtIndexForDebug(intAttr.getInt()); + if (!selected) + return op->emitError("'rtg.elaboration' attribute value out of bounds, " + "must be between 0 (incl.) and ") + << set->originalSize << " (excl.)"; + } else { + std::uniform_int_distribution dist( + 0, set->getAsArrayRef().size() - 1); + selected = set->getAsArrayRef()[dist(rng)]; + } + + state[op.getResult()] = selected; + return DeletionKind::Delete; + } + + FailureOr + visitOp(SetDifferenceOp op, function_ref addToWorklist) { + auto original = cast(state.at(op.getOriginal()))->getAsArrayRef(); + auto diff = cast(state.at(op.getDiff()))->getAsArrayRef(); + + SmallVector result; + std::set_difference(original.begin(), original.end(), diff.begin(), + diff.end(), std::inserter(result, result.end())); + + internalizeResult(op.getResult(), std::move(result), + options.debugMode); + return DeletionKind::Delete; + } + + LogicalResult elaborate(TestOp testOp) { + DenseSet visited; + std::deque worklist; + DenseSet toDelete; + for (auto &op : *testOp.getBody()) + if (op.use_empty()) + worklist.push_back(&op); + + while (!worklist.empty()) { + auto *curr = worklist.back(); + if (visited.contains(curr)) { + worklist.pop_back(); + continue; + } + + if (curr->getNumRegions() != 0) + return curr->emitError("nested regions not supported"); + + bool addedSomething = false; + for (auto val : curr->getOperands()) { + if (state.contains(val)) + continue; + + auto *defOp = val.getDefiningOp(); + assert(defOp && "cannot be a BlockArgument here"); + if (!visited.contains(defOp)) { + worklist.push_back(defOp); + addedSomething = true; + } + } + + if (addedSomething) + continue; + + auto addToWorklist = [&](Operation *op) { + if (op->use_empty()) + worklist.push_front(op); + }; + auto result = dispatchOpVisitor(curr, addToWorklist); + if (failed(result)) + return failure(); + + if (*result == DeletionKind::Delete) + toDelete.insert(curr); + + visited.insert(curr); + worklist.pop_back(); + } + + // FIXME: this assumes that we didn't query the opaque value from an + // interpreted elaborator value in a way that it can remain used in the IR. + for (auto *op : toDelete) { + op->dropAllUses(); + op->erase(); + } + + // Reduce max memory consumption and make sure the values cannot be accessed + // anymore because we deleted the ops above. + state.clear(); + interned.clear(); + + return success(); + } + +private: + const ElaborationPassOptions &options; + std::mt19937 rng; + + // A map used to intern elaborator values. We do this such that we can + // compare pointers when, e.g., computing set differences, uniquing the + // elements in a set, etc. Otherwise, we'd need to do a deep value comparison + // in those situations. + // Use a pointer as the key with custom MapInfo because of object slicing when + // inserting an object of a derived class of ElaboratorValue. + // The custom MapInfo makes sure that we do a value comparison instead of + // comparing the pointers. + DenseMap, InternMapInfo> + interned; + + // A map from SSA values to a pointer of an interned elaborator value. + DenseMap state; + + SymbolTable symTable; +}; + +//===----------------------------------------------------------------------===// +// Elaborator Pass +//===----------------------------------------------------------------------===// + +struct ElaborationPass + : public rtg::impl::ElaborationPassBase { + using Base::Base; + + void runOnOperation() override; + void cloneTargetsIntoTests(SymbolTable &table); +}; +} // end namespace + +void ElaborationPass::runOnOperation() { + ElaborationPassOptions options; + options.seed = seed; + options.debugMode = debugMode; + + auto moduleOp = getOperation(); + SymbolTable table(moduleOp); + + cloneTargetsIntoTests(table); + + Elaborator elaborator(table, options); + for (auto testOp : moduleOp.getOps()) + if (failed(elaborator.elaborate(testOp))) + return signalPassFailure(); +} + +void ElaborationPass::cloneTargetsIntoTests(SymbolTable &table) { + auto moduleOp = getOperation(); + for (auto target : llvm::make_early_inc_range(moduleOp.getOps())) { + for (auto test : moduleOp.getOps()) { + // If the test requries nothing from a target, we can always run it. + if (test.getTarget().getEntries().empty()) + continue; + + // If the target requirements do not match, skip this test + // TODO: allow target refinements, just not coarsening + if (target.getTarget() != test.getTarget()) + continue; + + IRRewriter rewriter(test); + // Create a new test for the matched target + auto newTest = cast(test->clone()); + newTest.setSymName(test.getSymName().str() + "_" + + target.getSymName().str()); + table.insert(newTest, rewriter.getInsertionPoint()); + + // Copy the target body into the newly created test + IRMapping mapping; + rewriter.setInsertionPointToStart(newTest.getBody()); + for (auto &op : target.getBody()->without_terminator()) + rewriter.clone(op, mapping); + + for (auto [returnVal, result] : + llvm::zip(target.getBody()->getTerminator()->getOperands(), + newTest.getBody()->getArguments())) + result.replaceAllUsesWith(mapping.lookup(returnVal)); + + newTest.getBody()->eraseArguments(0, + newTest.getBody()->getNumArguments()); + newTest.setTarget(DictType::get(&getContext(), {})); + } + + target->erase(); + } + + // Erase all remaining non-matched tests. + for (auto test : llvm::make_early_inc_range(moduleOp.getOps())) + if (!test.getTarget().getEntries().empty()) + test->erase(); +} diff --git a/test/Dialect/RTG/Transform/elaboration.mlir b/test/Dialect/RTG/Transform/elaboration.mlir new file mode 100644 index 000000000000..96c2c5e6e474 --- /dev/null +++ b/test/Dialect/RTG/Transform/elaboration.mlir @@ -0,0 +1,123 @@ +// RUN: circt-opt --rtg-elaborate="debug=true seed=0" --split-input-file --verify-diagnostics %s | FileCheck %s + +// CHECK-LABEL: rtg.sequence @seq0 +rtg.sequence @seq0 { + %2 = arith.constant 2 : i32 +} + +// CHECK-LABEL: rtg.sequence @seq2 +rtg.sequence @seq2 { +^bb0(%arg0: !rtg.sequence): + %0 = rtg.sequence_closure @seq0 + %set = rtg.set_create %arg0, %0 : !rtg.sequence + // expected-warning @below {{set contained 1 duplicate value(s), the value at index 0 might not be the intended one}} + %seq = rtg.set_select_random %set : !rtg.set {rtg.elaboration = 0} + rtg.invoke_sequence %seq + rtg.invoke_sequence %seq +} + +// Test the set operations and passing a sequence to another one via argument +// CHECK-LABEL: rtg.test @setOperations +rtg.test @setOperations : !rtg.dict<> { + // CHECK-NEXT: arith.constant 2 : i32 + // CHECK-NEXT: arith.constant 2 : i32 + // CHECK-NEXT: } + %0 = rtg.sequence_closure @seq0 + %1 = rtg.sequence_closure @seq2(%0 : !rtg.sequence) + %set = rtg.set_create %0, %1 : !rtg.sequence + %seq = rtg.set_select_random %set : !rtg.set {rtg.elaboration = 0} + %new_set = rtg.set_create %seq : !rtg.sequence + %diff = rtg.set_difference %set, %new_set : !rtg.set + %seq1 = rtg.set_select_random %diff : !rtg.set {rtg.elaboration = 0} + rtg.invoke_sequence %seq1 +} + +// CHECK-LABEL: rtg.sequence @seq3 +rtg.sequence @seq3 { +^bb0(%arg0: !rtg.set): + %seq = rtg.set_select_random %arg0 : !rtg.set {rtg.elaboration = 0} + rtg.invoke_sequence %seq +} + +// CHECK-LABEL: rtg.test @setArguments +rtg.test @setArguments : !rtg.dict<> { + // CHECK-NEXT: arith.constant 2 : i32 + // CHECK-NEXT: arith.constant 2 : i32 + // CHECK-NEXT: } + %0 = rtg.sequence_closure @seq0 + %1 = rtg.sequence_closure @seq2(%0 : !rtg.sequence) + %2 = rtg.set_create %1, %0 : !rtg.sequence + %3 = rtg.sequence_closure @seq3(%2 : !rtg.set) + rtg.invoke_sequence %3 +} + +// CHECK-LABEL: rtg.sequence @seq4 +rtg.sequence @seq4 { +^bb0(%arg0: !rtg.sequence): + %0 = rtg.sequence_closure @seq0 + %set = rtg.set_create %arg0, %0 : !rtg.sequence +} + +// Make sure we also delete ops that don't have any users and thus could be +// skipped and end up with null operands because the defining op was deleted due +// to other users. +// CHECK-LABEL: rtg.test @noNullOperands +rtg.test @noNullOperands : !rtg.dict<> { + // CHECK-NEXT: } + %1 = rtg.sequence_closure @seq0 + %2 = rtg.sequence_closure @seq4(%1 : !rtg.sequence) + rtg.invoke_sequence %2 +} + +rtg.target @target0 : !rtg.dict { + %0 = arith.constant 0 : i32 + rtg.yield %0 : i32 +} + +rtg.target @target1 : !rtg.dict { + %0 = arith.constant 1 : i32 + rtg.yield %0 : i32 +} + +// CHECK-LABEL: @targetTest_target0 +// CHECK: [[V0:%.+]] = arith.constant 0 +// CHECK: arith.addi [[V0]], [[V0]] + +// CHECK-LABEL: @targetTest_target1 +// CHECK: [[V0:%.+]] = arith.constant 1 +// CHECK: arith.addi [[V0]], [[V0]] +rtg.test @targetTest : !rtg.dict { +^bb0(%arg0: i32): + arith.addi %arg0, %arg0 : i32 +} + +// CHECK-NOT: @unmatchedTest +rtg.test @unmatchedTest : !rtg.dict { +^bb0(%arg0: i64): + arith.addi %arg0, %arg0 : i64 +} + +// ----- + +rtg.test @opaqueValuesAndSets : !rtg.dict<> { + %0 = arith.constant 2 : i32 + // expected-error @below {{cannot create a set of opaque values because they cannot be reliably uniqued}} + %1 = rtg.set_create %0 : i32 +} + +// ----- + +rtg.sequence @seq0 { + %2 = arith.constant 2 : i32 +} + +// Test that the elaborator value interning works as intended and exercise 'set_select_random' error messages. +rtg.test @setOperations : !rtg.dict<> { + %0 = rtg.sequence_closure @seq0 + %1 = rtg.sequence_closure @seq0 + %set = rtg.set_create %0, %1 : !rtg.sequence + // expected-warning @below {{set contained 1 duplicate value(s), the value at index 2 might not be the intended one}} + // expected-error @below {{'rtg.elaboration' attribute value out of bounds, must be between 0 (incl.) and 2 (excl.)}} + %seq = rtg.set_select_random %set : !rtg.set {rtg.elaboration = 2} + rtg.invoke_sequence %seq +}