Skip to content

Commit

Permalink
move GeneralizeNamedOp pass
Browse files Browse the repository at this point in the history
  • Loading branch information
PhaneeshB committed Nov 27, 2023
1 parent 36bcb7e commit ee88b49
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 15 deletions.
1 change: 1 addition & 0 deletions compiler/src/iree/compiler/Codegen/Common/GPU/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ iree_compiler_cc_library(
"GPUCheckResourceUsage.cpp",
"GPUDistribute.cpp",
"GPUDistributeSharedMemoryCopy.cpp",
"GPUGeneralizeNamedOps.cpp",
"GPUMultiBuffering.cpp",
"GPUPatterns.cpp",
"GPUPipelining.cpp",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ iree_cc_library(
"GPUCheckResourceUsage.cpp"
"GPUDistribute.cpp"
"GPUDistributeSharedMemoryCopy.cpp"
"GPUGeneralizeNamedOps.cpp"
"GPUMultiBuffering.cpp"
"GPUPatterns.cpp"
"GPUPipelining.cpp"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2023 The IREE Authors
//
// Licensed 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

//===- GPUGeneralizeNamedOps.cpp - Pass to generalize named linalg ops --===//
//
// The pass is to generalize named linalg ops that are better as linalg.generic
// ops in IREE
//
//===----------------------------------------------------------------------===//

#include "iree/compiler/Codegen/Common/GPU/PassDetail.h"
#include "iree/compiler/Codegen/Common/GPU/Passes.h"
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Dialect/Linalg/Transforms/Transforms.h"
#include "mlir/Pass/Pass.h"

namespace mlir {
namespace iree_compiler {

namespace {
struct GPUGeneralizeNamedOpsPass
: public GPUGeneralizeNamedOpsBase<GPUGeneralizeNamedOpsPass> {

void runOnOperation() override;
};
} // namespace

void GPUGeneralizeNamedOpsPass::runOnOperation() {
auto funcOp = getOperation();
SmallVector<linalg::LinalgOp> namedOpCandidates;
funcOp.walk([&](linalg::LinalgOp linalgOp) {
if (isa<linalg::BatchMatmulTransposeBOp, linalg::MatmulTransposeBOp,
linalg::VecmatOp, linalg::MatvecOp>(linalgOp))
namedOpCandidates.push_back(linalgOp);
});

IRRewriter rewriter(&getContext());
for (auto linalgOp : namedOpCandidates) {
rewriter.setInsertionPoint(linalgOp);
FailureOr<linalg::GenericOp> generalizedOp =
linalg::generalizeNamedOp(rewriter, linalgOp);
if (failed(generalizedOp)) {
linalgOp->emitOpError("failed to generalize operation");
return signalPassFailure();
}
}
}

std::unique_ptr<OperationPass<func::FuncOp>> createGPUGeneralizeNamedOpsPass() {
return std::make_unique<GPUGeneralizeNamedOpsPass>();
}

} // namespace iree_compiler
} // namespace mlir
3 changes: 3 additions & 0 deletions compiler/src/iree/compiler/Codegen/Common/GPU/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ createWorkgroupSpecializationPass();
std::unique_ptr<OperationPass<func::FuncOp>>
createWorkGroupSwizzle(unsigned swizzleLogTile = 0);

// This pass generalizes named Linalg ops that are better off as generics.
std::unique_ptr<OperationPass<func::FuncOp>> createGPUGeneralizeNamedOpsPass();

/// Register Common GPU passes.
void registerCodegenCommonGPUPasses();

Expand Down
6 changes: 6 additions & 0 deletions compiler/src/iree/compiler/Codegen/Common/GPU/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def GPUDistributeSharedMemoryCopy :
let constructor = "mlir::iree_compiler::createGPUDistributeSharedMemoryCopy()";
}

def GPUGeneralizeNamedOps :
Pass<"iree-codegen-gpu-generalize-named-ops", "func::FuncOp"> {
let summary = "Convert named Linalg ops to linalg.generic ops";
let constructor = "mlir::iree_compiler::createGPUGeneralizeNamedOpsPass()";
}

def GPUMultiBuffering :
Pass<"iree-codegen-gpu-multi-buffering", "func::FuncOp"> {
let summary = "Pass to do multi buffering.";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: iree-opt --split-input-file --iree-spirv-generalize-named-ops %s | FileCheck %s
// RUN: iree-opt --split-input-file --iree-codegen-gpu-generalize-named-ops %s | FileCheck %s

module {
func.func @transpose_matmul(%arg0: tensor<1x4096xf32>, %arg1: tensor<32000x4096xf32>) -> tensor<1x32000xf32> {
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/iree/compiler/Codegen/LLVMGPU/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,8 @@ void addGPUTransformDialectPasses(OpPassManager &passManager) {

void buildLLVMGPUCodegenConfigurationPassPipeline(OpPassManager &pm) {
addCommonTargetExecutablePreprocessingPasses(pm);
auto &nestedModulePM = pm.nest<ModuleOp>();
nestedModulePM.addNestedPass<func::FuncOp>(createGPUGeneralizeNamedOpsPass());
pm.addPass(createLLVMGPUSelectLoweringStrategyPass());
}

Expand Down
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 @@ -62,7 +62,6 @@ iree_compiler_cc_library(
"SPIRVEmulateI64.cpp",
"SPIRVEraseStorageBufferStaticShape.cpp",
"SPIRVFinalVectorLowering.cpp",
"SPIRVGeneralizeNamedOps.cpp",
"SPIRVInitialVectorLowering.cpp",
"SPIRVLowerExecutableTargetPass.cpp",
"SPIRVMapMemRefStorageClass.cpp",
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 @@ -61,7 +61,6 @@ iree_cc_library(
"SPIRVEmulateI64.cpp"
"SPIRVEraseStorageBufferStaticShape.cpp"
"SPIRVFinalVectorLowering.cpp"
"SPIRVGeneralizeNamedOps.cpp"
"SPIRVInitialVectorLowering.cpp"
"SPIRVLowerExecutableTargetPass.cpp"
"SPIRVMapMemRefStorageClass.cpp"
Expand Down
3 changes: 1 addition & 2 deletions compiler/src/iree/compiler/Codegen/SPIRV/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,7 @@ void addSPIRVTransformDialectPassPipeline(OpPassManager &pm) {
void buildSPIRVCodegenConfigurationPassPipeline(OpPassManager &pm) {
addCommonTargetExecutablePreprocessingPasses(pm);
auto &nestedModulePM = pm.nest<ModuleOp>();
nestedModulePM.addNestedPass<func::FuncOp>(
createSPIRVGeneralizeNamedOpsPass());
nestedModulePM.addNestedPass<func::FuncOp>(createGPUGeneralizeNamedOpsPass());
pm.addPass(createSPIRVSelectLoweringStrategyPass());
}

Expand Down
4 changes: 0 additions & 4 deletions compiler/src/iree/compiler/Codegen/SPIRV/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ createSPIRVEraseStorageBufferStaticShapePass();
std::unique_ptr<OperationPass<func::FuncOp>>
createSPIRVFoldProcessorIDUsesPass();

// This pass generalizes named Linalg ops that are better off as generics.
std::unique_ptr<OperationPass<func::FuncOp>>
createSPIRVGeneralizeNamedOpsPass();

/// Pass to set the lowering strategy for the target variant.
std::unique_ptr<OperationPass<IREE::HAL::ExecutableVariantOp>>
createSPIRVSelectLoweringStrategyPass();
Expand Down
6 changes: 0 additions & 6 deletions compiler/src/iree/compiler/Codegen/SPIRV/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ def SPIRVEraseStorageBufferStaticShape :
let constructor = "mlir::iree_compiler::createSPIRVEraseStorageBufferStaticShapePass()";
}

def SPIRVGeneralizeNamedOps :
Pass<"iree-spirv-generalize-named-ops", "func::FuncOp"> {
let summary = "Convert named Linalg ops to linalg.generic ops";
let constructor = "mlir::iree_compiler::createSPIRVGeneralizeNamedOpsPass()";
}

def SPIRVLowerExecutableTarget :
Pass<"iree-spirv-lower-executable-target-pass",
"mlir::iree_compiler::IREE::HAL::ExecutableVariantOp"> {
Expand Down

0 comments on commit ee88b49

Please sign in to comment.