|
| 1 | +// Copyright 2024 The IREE Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | + |
| 7 | +#include "iree/compiler/Codegen/Common/Passes.h" |
| 8 | +#include "iree/compiler/Codegen/Dialect/Codegen/IR/IREECodegenAttrs.h" |
| 9 | +#include "iree/compiler/Codegen/Utils/GPUUtils.h" |
| 10 | +#include "iree/compiler/Dialect/HAL/IR/HALTypes.h" |
| 11 | +#include "mlir/IR/Visitors.h" |
| 12 | +#include "mlir/Interfaces/FunctionInterfaces.h" |
| 13 | +#include "mlir/Interfaces/SideEffectInterfaces.h" |
| 14 | + |
| 15 | +namespace mlir::iree_compiler { |
| 16 | + |
| 17 | +#define GEN_PASS_DEF_VERIFYWORKGROUPDISTRIBUTIONPASS |
| 18 | +#include "iree/compiler/Codegen/Common/Passes.h.inc" |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +struct VerifyWorkgroupDistributionPass final |
| 23 | + : impl::VerifyWorkgroupDistributionPassBase< |
| 24 | + VerifyWorkgroupDistributionPass> { |
| 25 | + |
| 26 | + void runOnOperation() override { |
| 27 | + FunctionOpInterface funcOp = getOperation(); |
| 28 | + |
| 29 | + WalkResult hasForall = funcOp.walk([&](scf::ForallOp forallOp) { |
| 30 | + if (forallOpHasMappingType<IREE::Codegen::WorkgroupMappingAttr>( |
| 31 | + forallOp)) { |
| 32 | + return WalkResult::interrupt(); |
| 33 | + } |
| 34 | + return WalkResult::advance(); |
| 35 | + }); |
| 36 | + |
| 37 | + // Without a workgroup level forall, either this is a single workgroup |
| 38 | + // dispatch, in which case no verification is needed, or this is already |
| 39 | + // distributed in which case verification is no longer possible. |
| 40 | + if (!hasForall.wasInterrupted()) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + auto globalAddressSpace = IREE::HAL::DescriptorTypeAttr::get( |
| 45 | + &getContext(), IREE::HAL::DescriptorType::StorageBuffer); |
| 46 | + |
| 47 | + // Walk in PreOrder so that parent operations are visited before children, |
| 48 | + // thus allowing all operations contained within workgroup foralls to be |
| 49 | + // skipped. |
| 50 | + WalkResult res = funcOp.walk<WalkOrder::PreOrder>([&](Operation *op) { |
| 51 | + if (auto forallOp = dyn_cast<scf::ForallOp>(op)) { |
| 52 | + // Skip ops contained within forall ops with workgroup mappings. |
| 53 | + if (forallOpHasMappingType<IREE::Codegen::WorkgroupMappingAttr>( |
| 54 | + forallOp)) { |
| 55 | + return WalkResult::skip(); |
| 56 | + } |
| 57 | + } |
| 58 | + if (auto memoryEffectOp = dyn_cast<MemoryEffectOpInterface>(op)) { |
| 59 | + for (Value operand : memoryEffectOp->getOperands()) { |
| 60 | + auto type = dyn_cast<MemRefType>(operand.getType()); |
| 61 | + if (!type || |
| 62 | + !memoryEffectOp.getEffectOnValue<MemoryEffects::Write>(operand)) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + // Writes to non-global memory are fine. |
| 67 | + if (type.getMemorySpace() != globalAddressSpace) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + op->emitOpError( |
| 72 | + "write affecting operations on global resources are restricted " |
| 73 | + "to workgroup distributed contexts."); |
| 74 | + return WalkResult::interrupt(); |
| 75 | + } |
| 76 | + } |
| 77 | + return WalkResult::advance(); |
| 78 | + }); |
| 79 | + |
| 80 | + if (res.wasInterrupted()) { |
| 81 | + funcOp.emitOpError("failed on workgroup distribution verification"); |
| 82 | + return signalPassFailure(); |
| 83 | + } |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +} // namespace |
| 88 | + |
| 89 | +} // namespace mlir::iree_compiler |
0 commit comments