Files
TheRock/compiler/amd-llvm/mlir/lib/Transforms/GenerateRuntimeVerification.cpp
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

104 lines
3.5 KiB
C++

//===- RuntimeOpVerification.cpp - Op Verification ------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "mlir/IR/AsmState.h"
#include "mlir/Transforms/Passes.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/Operation.h"
#include "mlir/Interfaces/RuntimeVerifiableOpInterface.h"
namespace mlir {
#define GEN_PASS_DEF_GENERATERUNTIMEVERIFICATIONPASS
#include "mlir/Transforms/Passes.h.inc"
} // namespace mlir
using namespace mlir;
namespace {
struct GenerateRuntimeVerificationPass
: public impl::GenerateRuntimeVerificationPassBase<
GenerateRuntimeVerificationPass> {
using impl::GenerateRuntimeVerificationPassBase<
GenerateRuntimeVerificationPass>::GenerateRuntimeVerificationPassBase;
void runOnOperation() override;
};
/// Default error message generator for runtime verification failures.
///
/// This class generates error messages with different levels of verbosity:
/// - Level 0: Shows only the error message and operation location
/// - Level 1: Shows the full operation string, error message, and location
///
/// Clients can call getVerboseLevel() to retrieve the current verbose level
/// and use it to customize their own error message generators with similar
/// behavior patterns.
class DefaultErrMsgGenerator {
private:
unsigned vLevel;
AsmState &state;
public:
DefaultErrMsgGenerator(unsigned verboseLevel, AsmState &asmState)
: vLevel(verboseLevel), state(asmState) {}
std::string operator()(Operation *op, StringRef msg) {
std::string buffer;
llvm::raw_string_ostream stream(buffer);
stream << "ERROR: Runtime op verification failed\n";
if (vLevel == 1) {
op->print(stream, state);
stream << "\n^ " << msg;
} else {
stream << "^ " << msg;
}
stream << "\nLocation: ";
op->getLoc().print(stream);
return buffer;
}
unsigned getVerboseLevel() const { return vLevel; }
};
} // namespace
void GenerateRuntimeVerificationPass::runOnOperation() {
// Check verboseLevel is in range [0, 1].
if (verboseLevel > 1) {
getOperation()->emitError(
"generate-runtime-verification pass: set verboseLevel to 0 or 1");
signalPassFailure();
return;
}
// The implementation of the RuntimeVerifiableOpInterface may create ops that
// can be verified. We don't want to generate verification for IR that
// performs verification, so gather all runtime-verifiable ops first.
SmallVector<RuntimeVerifiableOpInterface> ops;
getOperation()->walk([&](RuntimeVerifiableOpInterface verifiableOp) {
ops.push_back(verifiableOp);
});
// We may generate a lot of error messages and so we need to ensure the
// printing is fast.
OpPrintingFlags flags;
flags.elideLargeElementsAttrs();
flags.skipRegions();
flags.useLocalScope();
AsmState state(getOperation(), flags);
// Client can call getVerboseLevel() to fetch verbose level.
DefaultErrMsgGenerator defaultErrMsgGenerator(verboseLevel.getValue(), state);
OpBuilder builder(getOperation()->getContext());
for (RuntimeVerifiableOpInterface verifiableOp : ops) {
builder.setInsertionPoint(verifiableOp);
verifiableOp.generateRuntimeVerification(builder, verifiableOp.getLoc(),
defaultErrMsgGenerator);
};
}