Skip to content

Commit

Permalink
[SPIRV] Switch to new pass generation tablegen definitions. (#18214)
Browse files Browse the repository at this point in the history
This is mostly NFC. The revision applies additional cleanups:

- Remove two outdated passes: SPIRVEmulateBf16 and
SPIRVFoldProcessorIDUses. They do not have any implementation. Removing
the declaration is free.
- Combine private members and options in SPIRVTileAndPromote.
- Switch a couple of passes to follow `create.*Pass` naming convention.

---------

Signed-off-by: hanhanW <[email protected]>
  • Loading branch information
hanhanW authored Aug 14, 2024
1 parent a72e78b commit 7cac1b2
Show file tree
Hide file tree
Showing 26 changed files with 174 additions and 383 deletions.
1 change: 0 additions & 1 deletion compiler/src/iree/compiler/Codegen/SPIRV/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ iree_gentbl_cc_library(
iree_compiler_cc_library(
name = "PassHeaders",
hdrs = [
"PassDetail.h",
"Passes.h",
"Passes.h.inc",
],
Expand Down
1 change: 0 additions & 1 deletion compiler/src/iree/compiler/Codegen/SPIRV/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ iree_cc_library(
NAME
PassHeaders
HDRS
"PassDetail.h"
"Passes.h"
"Passes.h.inc"
DEPS
Expand Down
13 changes: 9 additions & 4 deletions compiler/src/iree/compiler/Codegen/SPIRV/ConvertToSPIRVPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <cstdint>
#include <tuple>

#include "iree/compiler/Codegen/SPIRV/PassDetail.h"
#include "iree/compiler/Codegen/SPIRV/Passes.h"
#include "iree/compiler/Codegen/SPIRV/Utils.h"
#include "iree/compiler/Codegen/Utils/Utils.h"
Expand Down Expand Up @@ -64,6 +63,9 @@

namespace mlir::iree_compiler {

#define GEN_PASS_DEF_CONVERTTOSPIRVPASS
#include "iree/compiler/Codegen/SPIRV/Passes.h.inc"

namespace {

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -470,14 +472,17 @@ struct RemoveIdentityConversionCast final
/// Converts remaining interface ops into SPIR-V global variables, GPU processor
/// ID ops into SPIR-V global variables, loop/standard ops into corresponding
/// SPIR-V ops.
class ConvertToSPIRVPass final : public ConvertToSPIRVBase<ConvertToSPIRVPass> {
class ConvertToSPIRVPass final
: public impl::ConvertToSPIRVPassBase<ConvertToSPIRVPass> {
public:
using impl::ConvertToSPIRVPassBase<
ConvertToSPIRVPass>::ConvertToSPIRVPassBase;
explicit ConvertToSPIRVPass(unsigned indexBits) : indexBits(indexBits) {}

void getDependentDialects(DialectRegistry &registry) const override {
registry.insert<spirv::SPIRVDialect>();
}

explicit ConvertToSPIRVPass(unsigned indexBits) : indexBits(indexBits) {}

LogicalResult initializeOptions(
StringRef options,
function_ref<LogicalResult(const Twine &)> errorHandler) override {
Expand Down
23 changes: 0 additions & 23 deletions compiler/src/iree/compiler/Codegen/SPIRV/PassDetail.h

This file was deleted.

9 changes: 5 additions & 4 deletions compiler/src/iree/compiler/Codegen/SPIRV/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ static void addMemRefLoweringPasses(OpPassManager &modulePassManager) {
// Turn scalar load/store from memrefs into vectorized ones if possible.
// This gives better memory access patterns, which is very important for
// perf.
.addPass(createSPIRVVectorizeLoadStore)
.addPass(createSPIRVVectorizeLoadStorePass)
// Perform optimizations that need to across the scf.for region boundary.
.addPass(createForOpCanonicalizationPass)
// Perform various vector-level cross-op optimizations like load-store
Expand Down Expand Up @@ -385,8 +385,9 @@ void addSPIRVCooperativeMatrixVectorizePassPipeline(
addBufferizePasses(funcPassManager, gpuAllocateWorkgroupMemoryFn);

// Tile to GPU workgroups and promote.
funcPassManager.addPass(createSPIRVTileAndPromotePass(
/*promoteCMatrix=*/true, /*skipThreadLevel=*/true));
funcPassManager.addPass(
createSPIRVTileAndPromotePass(SPIRVTileAndPromotePassOptions{
/*promoteCMatrix=*/true, /*skipThreadLevel=*/true}));
funcPassManager.addPass(createRemoveSingleIterationLoopPass());
// Run canonicalization patterns to propagate constant shape sizes after
// removing trip-one loops.
Expand Down Expand Up @@ -443,7 +444,7 @@ void addSPIRVCooperativeMatrixVectorizePassPipeline(
funcPassManager.addPass(createForOpCanonicalizationPass());
funcPassManager.addPass(createCanonicalizerPass());
funcPassManager.addPass(createCSEPass());
funcPassManager.addPass(createSPIRVVectorToGPUSubgroupMMAOpsPass());
funcPassManager.addPass(createSPIRVVectorToGPUSubgroupMMAPass());
funcPassManager.addPass(createCanonicalizerPass());
funcPassManager.addPass(createCSEPass());
addSPIRVVectorLoweringPasses(funcPassManager);
Expand Down
125 changes: 15 additions & 110 deletions compiler/src/iree/compiler/Codegen/SPIRV/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,116 +65,6 @@ void buildSPIRVCodegenPassPipeline(OpPassManager &variantPassManager);
/// Populates passes needed to link HAL executables across SPIRV targets.
void buildSPIRVLinkingPassPipeline(OpPassManager &modulePassManager);

//===---------------------------------------------------------------------===//
// SPIR-V passes
//===---------------------------------------------------------------------===//

/// Pass to perform the final conversion to SPIR-V dialect.
///
/// This pass converts remaining interface ops into SPIR-V global variables,
/// GPU processor ID ops into SPIR-V global variables, loop/standard ops into
/// corresponding SPIR-V ops.
std::unique_ptr<OperationPass<ModuleOp>>
createConvertToSPIRVPass(unsigned indexWidth = 32);

/// Annotates the innermost Winograd loops with the spirv distribute
/// attribute.
std::unique_ptr<InterfacePass<FunctionOpInterface>>
createSPIRVAnnotateWinogradLoopsPass();

/// Breaks down large vectors not natively supported by SPIR-V.
std::unique_ptr<InterfacePass<FunctionOpInterface>>
createSPIRVBreakDownLargeVectorPass();

// Converts #iree_gpu.target into #spirv.target_env.
std::unique_ptr<OperationPass<ModuleOp>> createSPIRVConvertGPUTargetPass();

/// Emulates bfloat 16 ops with 32-bit float ops.
std::unique_ptr<InterfacePass<FunctionOpInterface>>
createSPIRVEmulateBf16Pass();

/// Emulates 64-bit integer ops with 32-bit integer ops.
std::unique_ptr<InterfacePass<FunctionOpInterface>> createSPIRVEmulateI64Pass();

/// Turns static shaped storage buffer subspan ops into dynamic shaped ones.
std::unique_ptr<InterfacePass<FunctionOpInterface>>
createSPIRVEraseStorageBufferStaticShapePass();

/// Pass to perform final vector ops lowering to meet SPIR-V requirements.
std::unique_ptr<InterfacePass<FunctionOpInterface>>
createSPIRVFinalVectorLoweringPass();

/// Creates a pass to fold processor ID uses where possible.
std::unique_ptr<InterfacePass<FunctionOpInterface>>
createSPIRVFoldProcessorIDUsesPass();

/// Pass to perform initial vector ops lowering to meet SPIR-V requirements.
std::unique_ptr<InterfacePass<FunctionOpInterface>>
createSPIRVInitialVectorLoweringPass();

/// Links SPIR-V HAL executables within the top-level program module.
std::unique_ptr<OperationPass<ModuleOp>> createSPIRVLinkExecutablesPass();

/// Pass to set the lowering strategy for the target variant.
std::unique_ptr<OperationPass<ModuleOp>>
createSPIRVSelectLoweringStrategyPass();

/// Main pass to lower executables to scalar + vector code on SPIR-V path.
/// Invokes one of the pass pipelines that translate the executable to
/// scalar + vector code.
std::unique_ptr<InterfacePass<FunctionOpInterface>>
createSPIRVLowerExecutableTargetPass();

/// Pass to lower executables using Transform dialect on the SPIR-V backend.
/// This shouldnt be a separate pass, but it is since there are some
/// extra spir-v passes that need to be run as well.
std::unique_ptr<OperationPass<ModuleOp>>
createSPIRVLowerExecutableUsingTransformDialectPass();

/// Pass to map MemRef memory spaces to SPIR-V storage classes.
std::unique_ptr<InterfacePass<FunctionOpInterface>>
createSPIRVMapMemRefStorageClassPass();

/// Pass to materialize SPIR-V target requirements of hal.exectuable.variant
/// ops into hal.executable.condition regions.
std::unique_ptr<OperationPass<IREE::HAL::ExecutableVariantOp>>
createSPIRVMaterializeExecutableConditionsPass();

/// Pass to tile and distribute Linalg ops with buffer semantics to
/// invocations.
std::unique_ptr<InterfacePass<FunctionOpInterface>>
createSPIRVTileAndDistributePass();

/// Pass to promote Linalg ops with buffer semantics to use workgroup memory
/// and then tile to invocations.
std::unique_ptr<InterfacePass<FunctionOpInterface>>
createSPIRVTileAndPromotePass(bool promoteCMatrix = false,
bool skipThreadLevel = false);

/// Pass to tile Linalg ops with buffer semantics suitable for lowering to
/// SPIR-V cooperative ops.
std::unique_ptr<InterfacePass<FunctionOpInterface>>
createSPIRVTileToCooperativeOpsPass();

// Trims the SPIR-V target environment of a HAL executable variant to the
// minimal requirement per the compiled spirv.module op needs.
std::unique_ptr<OperationPass<IREE::HAL::ExecutableVariantOp>>
createSPIRVTrimExecutableTargetEnvPass();

/// Converts vector ops to gpu subgroup MMA ops.
std::unique_ptr<InterfacePass<FunctionOpInterface>>
createSPIRVVectorToGPUSubgroupMMAOpsPass();

/// Converts memref of scalar to memref of vector of efficent size. This will
/// allow to convert memory accesses to vector load/store in SPIR-V without
/// having pointer bitcast.
std::unique_ptr<InterfacePass<FunctionOpInterface>>
createSPIRVVectorizeLoadStore();

/// Pass to do vectorization suitable for lowering to SPIR-V cooperative ops.
std::unique_ptr<InterfacePass<FunctionOpInterface>>
createSPIRVVectorizeToCooperativeOpsPass();

/// Pass pipeline to lower IREE HAL executables by tiling and distributing to
/// workgroups and invocations and vectorizing. Each invocation handles a
/// vector.
Expand All @@ -200,10 +90,25 @@ LogicalResult verifySPIRVMatmulPromoteVectorizePassPipeline(
IREE::Codegen::TranslationInfoAttr translationInfo,
ArrayRef<int64_t> workgroupSize);

//===---------------------------------------------------------------------===//
// Wrappers that not use tablegen options.
//===---------------------------------------------------------------------===//

/// Pass to perform the final conversion to SPIR-V dialect.
///
/// This pass converts remaining interface ops into SPIR-V global variables,
/// GPU processor ID ops into SPIR-V global variables, loop/standard ops into
/// corresponding SPIR-V ops.
std::unique_ptr<OperationPass<ModuleOp>>
createConvertToSPIRVPass(unsigned indexWidth);

//----------------------------------------------------------------------------//
// Registration
//----------------------------------------------------------------------------//

#define GEN_PASS_DECL
#include "iree/compiler/Codegen/SPIRV/Passes.h.inc" // IWYU pragma: keep

void registerCodegenSPIRVPasses();

} // namespace mlir::iree_compiler
Expand Down
Loading

0 comments on commit 7cac1b2

Please sign in to comment.