Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Codegen][Tuner] attr verifier for tuning specs #19486

Merged
merged 9 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ getUserTuningSpec(ModuleOp module, IREE::Codegen::IREECodegenDialect &dialect) {
<< clCodegenTuningSpecPath;
}

// Iterate through all operations in the module to verify attributes
bangtianliu marked this conversation as resolved.
Show resolved Hide resolved
for (Operation &op : (*maybeTransformLibrary).getBody()->getOperations()) {
for (NamedAttribute attr : op.getAttrs()) {
if (failed(dialect.verifyOperationAttribute(&op, attr))) {
return op.emitError() << "Attribute verification failed for operation "
"in the user tuning spec";
}
}
}
bangtianliu marked this conversation as resolved.
Show resolved Hide resolved

return *maybeTransformLibrary;
}

Expand Down Expand Up @@ -138,8 +148,25 @@ getDefaultTuningSpec(ModuleOp module,

// Load the library through the codegen dialect so that we cache the parsed
// module.
return dialect.getOrParseTransformLibraryModule(defaultTuningSpecName,
*defaultTuningSpecSource);
FailureOr<ModuleOp> defaultTransformLibrary =
dialect.getOrParseTransformLibraryModule(defaultTuningSpecName,
*defaultTuningSpecSource);
if (failed(defaultTransformLibrary)) {
return module->emitError()
<< "Failed to parse default tuning spec transform library for '"
<< arch << "'";
}
// Iterate through operations and validate their attributes
for (Operation &op : (*defaultTransformLibrary).getBody()->getOperations()) {
for (NamedAttribute attr : op.getAttrs()) {
if (failed(dialect.verifyOperationAttribute(&op, attr))) {
return op.emitError() << "Attribute verification failed for operation "
"in default tuning spec";
}
}
}
bangtianliu marked this conversation as resolved.
Show resolved Hide resolved

return *defaultTransformLibrary;
bangtianliu marked this conversation as resolved.
Show resolved Hide resolved
}

static FailureOr<DenseElementsAttr>
Expand Down Expand Up @@ -240,6 +267,17 @@ struct MaterializeTuningSpecsPass final
return signalPassFailure();
}

// Iterate through operations in the linked module and verify attributes
for (Operation &op : (linkedTuningSpec.get()).getBody()->getOperations()) {
for (NamedAttribute attr : op.getAttrs()) {
if (failed(dialect->verifyOperationAttribute(&op, attr))) {
op.emitError("Attribute verification failed for operation in linked "
"tuning spec");
return signalPassFailure();
}
}
}

if (failed(dumpFinalTuningSpecToDir(linkedTuningSpec.get()))) {
return signalPassFailure();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ iree_lit_test_suite(
"vector_layout_analysis.mlir",
"vectorize_memref_copy.mlir",
"vectorize_tensor_pad.mlir",
"verify_tuning_specs.mlir",
"verify_workgroup_distribution.mlir",
"vmvx_materialize_encoding.mlir",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ iree_lit_test_suite(
"vector_layout_analysis.mlir"
"vectorize_memref_copy.mlir"
"vectorize_tensor_pad.mlir"
"verify_tuning_specs.mlir"
"verify_workgroup_distribution.mlir"
"vmvx_materialize_encoding.mlir"
TOOLS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: iree-opt --no-implicit-module --verify-diagnostics -split-input-file --mlir-disable-threading %s
bangtianliu marked this conversation as resolved.
Show resolved Hide resolved

module @td_module attributes { transform.with_named_sequence } {
module @foo_module attributes { transform.with_named_sequence } {
// expected-error @+1{{'iree_codegen.tuning_spec_entrypoint' attribute must be a UnitAttr}}
transform.named_sequence @foo(%arg0: !transform.any_op {transform.readonly}) -> !transform.any_op
bangtianliu marked this conversation as resolved.
Show resolved Hide resolved
attributes { iree_codegen.tuning_spec_entrypoint = "foo" } {
transform.yield %arg0 : !transform.any_op
}
transform.named_sequence @bar(%arg0: !transform.any_op {transform.readonly}) -> !transform.any_op
attributes { iree_codegen.tuning_spec_entrypoint } {
transform.yield %arg0 : !transform.any_op
}
func.func @baz(%arg0: i32) -> () {
return
}
bangtianliu marked this conversation as resolved.
Show resolved Hide resolved
}
}
bangtianliu marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "iree/compiler/Codegen/Dialect/Codegen/IR/IREECodegenDialect.cpp.inc"
#include "iree/compiler/Codegen/Dialect/Codegen/IR/IREECodegenOps.h"
#include "iree/compiler/Codegen/Dialect/Codegen/IR/UKernelOps.h"
#include "mlir/Dialect/Transform/IR/TransformOps.h"
#include "mlir/IR/DialectImplementation.h"

namespace mlir::iree_compiler::IREE::Codegen {
Expand Down Expand Up @@ -45,4 +46,39 @@ void IREECodegenDialect::initialize() {
>();
}

LogicalResult
IREECodegenDialect::verifyOperationAttribute(Operation *op,
NamedAttribute attribute) {
StringRef symbol = attribute.getName().strref();
Attribute attr = attribute.getValue();

// Early return if the symbol is not "iree_codegen.tuning_spec_entrypoint"
bangtianliu marked this conversation as resolved.
Show resolved Hide resolved
if (symbol != kTuningSpecEntrypointAttrName)
return success();

// Verify that the attribute is a UnitAttr
bangtianliu marked this conversation as resolved.
Show resolved Hide resolved
if (!llvm::isa<UnitAttr>(attr)) {
bangtianliu marked this conversation as resolved.
Show resolved Hide resolved
return op->emitError("'") << symbol << "' attribute must be a UnitAttr";
}

if (auto namedSeqOp = dyn_cast<transform::NamedSequenceOp>(op)) {
ArrayRef<Type> resTypes = namedSeqOp.getFunctionType().getResults();
if (resTypes.size() != 1 || !isa<transform::AnyOpType>(resTypes[0])) {
return namedSeqOp.emitWarning()
<< "Tuning spec entry point expected to return any_op";
}

ArrayRef<Type> argTypes = namedSeqOp.getArgumentTypes();
if (argTypes.size() != 1 || !isa<transform::AnyOpType>(argTypes[0])) {
return namedSeqOp.emitWarning()
<< "Tuning spec entry point expected to have a "
"single any_op argument";
}
bangtianliu marked this conversation as resolved.
Show resolved Hide resolved

return success();
bangtianliu marked this conversation as resolved.
Show resolved Hide resolved
}

return success();
}

} // namespace mlir::iree_compiler::IREE::Codegen
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def IREECodegen_Dialect : Dialect {
std::mutex libraryMutex;
}];
let useDefaultAttributePrinterParser = 1;
let hasOperationAttrVerify = 1;
}

def AnyRankedTensorOrMemRefType : AnyTypeOf<[AnyRankedTensor, AnyMemRef]>;
Expand Down
Loading