-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[VectorDistribution] Configure contraction layouts at linalg level
- Loading branch information
Showing
23 changed files
with
432 additions
and
376 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
compiler/src/iree/compiler/Codegen/LLVMGPU/LLVMGPUConfigureTensorLayouts.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// Copyright 2024 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 | ||
|
||
#include <algorithm> | ||
|
||
#include "iree/compiler/Codegen/Dialect/Codegen/IR/IREECodegenAttrs.h" | ||
#include "iree/compiler/Codegen/Dialect/GPU/IR/IREEGPUAttrs.h" | ||
#include "iree/compiler/Codegen/Dialect/VectorExt/IR/VectorExtDialect.h" | ||
#include "iree/compiler/Codegen/LLVMGPU/PassDetail.h" | ||
#include "iree/compiler/Codegen/LLVMGPU/Passes.h" | ||
#include "iree/compiler/Codegen/Utils/GPUUtils.h" | ||
#include "llvm/ADT/SetVector.h" | ||
#include "llvm/Support/Debug.h" | ||
#include "llvm/Support/MathExtras.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
#include "mlir/Analysis/SliceAnalysis.h" | ||
#include "mlir/Dialect/Vector/IR/VectorOps.h" | ||
#include "mlir/IR/BuiltinAttributes.h" | ||
#include "mlir/IR/BuiltinTypes.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "mlir/IR/TypeUtilities.h" | ||
|
||
#define DEBUG_TYPE "iree-llvmgpu-configure-vector-layouts" | ||
|
||
namespace mlir::iree_compiler { | ||
|
||
namespace { | ||
|
||
LogicalResult setContractionAnchor(IREE::GPU::MMAScheduleAttr schedule, | ||
RewriterBase &rewriter, | ||
linalg::GenericOp contract) { | ||
// TODO: Add SIMT fallback. | ||
if (!schedule) { | ||
return contract->emitError("missing mma schedule for contraction"); | ||
} | ||
|
||
// This function should have only be called on a contraction op. | ||
assert(linalg::isaContractionOpInterface(contract) && | ||
"cannot set contraction anchor on non contraction op"); | ||
|
||
auto layouts = schedule.getContractionLayout(contract); | ||
if (failed(layouts)) { | ||
return contract->emitError("cannot get concrete layout for contraction"); | ||
} | ||
|
||
auto [aLayout, bLayout, cLayout] = *layouts; | ||
Location loc = contract.getLoc(); | ||
|
||
Value lhs = contract.getOperand(0); | ||
Value rhs = contract.getOperand(1); | ||
Value acc = contract.getOperand(2); | ||
|
||
// Set layouts for lhs, rhs and acc. | ||
rewriter.setInsertionPoint(contract); | ||
auto layoutedLhs = rewriter.create<IREE::VectorExt::ToLayoutOp>( | ||
loc, lhs.getType(), lhs, aLayout); | ||
auto layoutedRhs = rewriter.create<IREE::VectorExt::ToLayoutOp>( | ||
loc, rhs.getType(), rhs, bLayout); | ||
auto layoutedAcc = rewriter.create<IREE::VectorExt::ToLayoutOp>( | ||
loc, acc.getType(), acc, cLayout); | ||
|
||
// Promote matmul lhs and rhs. | ||
// TODO: We should read this from the lowering_config on the operation. | ||
// TODO: This is a hack until layout analysis is improved. The layout analysis | ||
// should decide where to put these shared memory conversions. | ||
layoutedLhs->setAttr("shared_memory_conversion", rewriter.getUnitAttr()); | ||
layoutedRhs->setAttr("shared_memory_conversion", rewriter.getUnitAttr()); | ||
|
||
contract->setOperand(0, layoutedLhs.getResult()); | ||
contract->setOperand(1, layoutedRhs.getResult()); | ||
contract->setOperand(2, layoutedAcc.getResult()); | ||
|
||
// Set layout for result. | ||
rewriter.setInsertionPointAfter(contract); | ||
auto toLayout = rewriter.create<IREE::VectorExt::ToLayoutOp>( | ||
loc, contract.getResult(0).getType(), contract.getResult(0), cLayout); | ||
rewriter.replaceAllUsesExcept(contract.getResult(0), toLayout.getResult(), | ||
toLayout); | ||
|
||
return success(); | ||
} | ||
|
||
struct LLVMGPUConfigureTensorLayoutsPass | ||
: public LLVMGPUConfigureTensorLayoutsBase< | ||
LLVMGPUConfigureTensorLayoutsPass> { | ||
public: | ||
void getDependentDialects(DialectRegistry ®istry) const override { | ||
registry.insert<IREE::VectorExt::IREEVectorExtDialect>(); | ||
registry.insert<vector::VectorDialect>(); | ||
} | ||
|
||
void runOnOperation() override { | ||
auto func = getOperation(); | ||
|
||
std::array<int64_t, 3> workgroupSize; | ||
if (func->hasAttr("workgroup_size")) { | ||
auto tmpSizes = | ||
llvm::cast<ArrayAttr>(func->getAttr("workgroup_size")).getValue(); | ||
for (auto [i, size] : llvm::enumerate(tmpSizes)) { | ||
workgroupSize[i] = llvm::cast<IntegerAttr>(size).getInt(); | ||
} | ||
} else { | ||
std::optional<SmallVector<int64_t>> maybeWorkgroupSize = | ||
getWorkgroupSize(func); | ||
if (!maybeWorkgroupSize) { | ||
func->emitOpError() | ||
<< "unable to query workgroup_size information from entry point"; | ||
return signalPassFailure(); | ||
} | ||
for (auto [index, value] : llvm::enumerate(maybeWorkgroupSize.value())) { | ||
workgroupSize[index] = value; | ||
} | ||
for (auto index : llvm::seq<size_t>(maybeWorkgroupSize->size(), 3)) { | ||
workgroupSize[index] = 1; | ||
} | ||
} | ||
|
||
llvm::StringLiteral scheduleAttrName = | ||
IREE::GPU::MMAScheduleAttr::getMnemonic(); | ||
auto scheduleAttr = | ||
func->getAttrOfType<IREE::GPU::MMAScheduleAttr>(scheduleAttrName); | ||
if (!scheduleAttr) { | ||
DictionaryAttr configDict = getTranslationInfo(func).getConfiguration(); | ||
scheduleAttr = dyn_cast_or_null<IREE::GPU::MMAScheduleAttr>( | ||
configDict.get(scheduleAttrName)); | ||
} | ||
|
||
// Vector layout option setter aimed at contractions. For now, layout | ||
// setting for other problems like reductions is TODO. | ||
SmallVector<linalg::GenericOp> contracts; | ||
|
||
func->walk([&](linalg::GenericOp linalgOp) { | ||
if (linalg::isaContractionOpInterface(linalgOp)) { | ||
contracts.push_back(linalgOp); | ||
} | ||
}); | ||
|
||
IRRewriter rewriter(func); | ||
|
||
for (linalg::GenericOp contract : contracts) { | ||
if (failed(setContractionAnchor(scheduleAttr, rewriter, contract))) { | ||
return signalPassFailure(); | ||
} | ||
} | ||
} | ||
}; | ||
} // namespace | ||
|
||
std::unique_ptr<InterfacePass<mlir::FunctionOpInterface>> | ||
createLLVMGPUConfigureTensorLayouts() { | ||
return std::make_unique<LLVMGPUConfigureTensorLayoutsPass>(); | ||
} | ||
|
||
} // namespace mlir::iree_compiler |
Oops, something went wrong.