Skip to content

[mlir][spirv] Allow disabling control flow structurization #140561

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

Merged
merged 2 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 9 additions & 2 deletions mlir/include/mlir/Target/SPIRV/Deserialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ class MLIRContext;
namespace spirv {
class ModuleOp;

struct DeserializationOptions {
// Whether to structurize control flow into `spirv.mlir.selection` and
// `spirv.mlir.loop`.
bool enableControlFlowStructurization = true;
};

/// Deserializes the given SPIR-V `binary` module and creates a MLIR ModuleOp
/// in the given `context`. Returns the ModuleOp on success; otherwise, reports
/// errors to the error handler registered with `context` and returns a null
/// module.
OwningOpRef<spirv::ModuleOp> deserialize(ArrayRef<uint32_t> binary,
MLIRContext *context);
OwningOpRef<spirv::ModuleOp>
deserialize(ArrayRef<uint32_t> binary, MLIRContext *context,
const DeserializationOptions &options = {});

} // namespace spirv
} // namespace mlir
Expand Down
7 changes: 4 additions & 3 deletions mlir/lib/Target/SPIRV/Deserialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@

using namespace mlir;

OwningOpRef<spirv::ModuleOp> spirv::deserialize(ArrayRef<uint32_t> binary,
MLIRContext *context) {
Deserializer deserializer(binary, context);
OwningOpRef<spirv::ModuleOp>
spirv::deserialize(ArrayRef<uint32_t> binary, MLIRContext *context,
const DeserializationOptions &options) {
Deserializer deserializer(binary, context, options);

if (failed(deserializer.deserialize()))
return nullptr;
Expand Down
15 changes: 13 additions & 2 deletions mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ static inline bool isFnEntryBlock(Block *block) {
//===----------------------------------------------------------------------===//

spirv::Deserializer::Deserializer(ArrayRef<uint32_t> binary,
MLIRContext *context)
MLIRContext *context,
const spirv::DeserializationOptions &options)
: binary(binary), context(context), unknownLoc(UnknownLoc::get(context)),
module(createModuleOp()), opBuilder(module->getRegion())
module(createModuleOp()), opBuilder(module->getRegion()), options(options)
#ifndef NDEBUG
,
logger(llvm::dbgs())
Expand Down Expand Up @@ -2361,6 +2362,16 @@ LogicalResult spirv::Deserializer::splitConditionalBlocks() {
}

LogicalResult spirv::Deserializer::structurizeControlFlow() {
if (!options.enableControlFlowStructurization) {
LLVM_DEBUG(
{
logger.startLine()
<< "//----- [cf] skip structurizing control flow -----//\n";
logger.indent();
});
return success();
}

LLVM_DEBUG({
logger.startLine()
<< "//----- [cf] start structurizing control flow -----//\n";
Expand Down
7 changes: 6 additions & 1 deletion mlir/lib/Target/SPIRV/Deserialization/Deserializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "mlir/Dialect/SPIRV/IR/SPIRVEnums.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
#include "mlir/IR/Builders.h"
#include "mlir/Target/SPIRV/Deserialization.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/StringRef.h"
Expand Down Expand Up @@ -121,7 +122,8 @@ class Deserializer {
public:
/// Creates a deserializer for the given SPIR-V `binary` module.
/// The SPIR-V ModuleOp will be created into `context.
explicit Deserializer(ArrayRef<uint32_t> binary, MLIRContext *context);
explicit Deserializer(ArrayRef<uint32_t> binary, MLIRContext *context,
const DeserializationOptions &options);

/// Deserializes the remembered SPIR-V binary module.
LogicalResult deserialize();
Expand Down Expand Up @@ -622,6 +624,9 @@ class Deserializer {
/// A list of all structs which have unresolved member types.
SmallVector<DeferredStructTypeInfo, 0> deferredStructTypesInfos;

/// Deserialization options.
DeserializationOptions options;

#ifndef NDEBUG
/// A logger used to emit information during the deserialzation process.
llvm::ScopedPrinter logger;
Expand Down
16 changes: 13 additions & 3 deletions mlir/lib/Target/SPIRV/TranslateRegistration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ using namespace mlir;
// Deserializes the SPIR-V binary module stored in the file named as
// `inputFilename` and returns a module containing the SPIR-V module.
static OwningOpRef<Operation *>
deserializeModule(const llvm::MemoryBuffer *input, MLIRContext *context) {
deserializeModule(const llvm::MemoryBuffer *input, MLIRContext *context,
const spirv::DeserializationOptions &options) {
context->loadDialect<spirv::SPIRVDialect>();

// Make sure the input stream can be treated as a stream of SPIR-V words
Expand All @@ -51,17 +52,26 @@ deserializeModule(const llvm::MemoryBuffer *input, MLIRContext *context) {

auto binary = llvm::ArrayRef(reinterpret_cast<const uint32_t *>(start),
size / sizeof(uint32_t));
return spirv::deserialize(binary, context);
return spirv::deserialize(binary, context, options);
}

namespace mlir {
void registerFromSPIRVTranslation() {
static llvm::cl::opt<bool> enableControlFlowStructurization(
"spirv-structurize-control-flow",
llvm::cl::desc(
"Enable control flow structurization into `spirv.mlir.selection` and "
"`spirv.mlir.loop`. This may need to be disabled to support "
"deserialization of early exits (see #138688)"),
llvm::cl::init(true));

TranslateToMLIRRegistration fromBinary(
"deserialize-spirv", "deserializes the SPIR-V module",
[](llvm::SourceMgr &sourceMgr, MLIRContext *context) {
assert(sourceMgr.getNumBuffers() == 1 && "expected one buffer");
return deserializeModule(
sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()), context);
sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()), context,
{enableControlFlowStructurization});
});
}
} // namespace mlir
Expand Down