|
| 1 | +//===-- XeVMAttachTarget.cpp - DESC -----------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This file is 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 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file implements the `GpuXeVMAttachTarget` pass, attaching `#xevm.target` |
| 10 | +// attributes to GPU modules. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include "gc/Dialect/LLVMIR/XeVMDialect.h" |
| 15 | + |
| 16 | +#include "gc/Target/LLVM/XeVM/Target.h" |
| 17 | +#include "gc/Transforms/Passes.h" |
| 18 | + |
| 19 | +#include "mlir/Dialect/GPU/IR/GPUDialect.h" |
| 20 | +#include "mlir/Dialect/GPU/Transforms/Passes.h" |
| 21 | +#include "mlir/IR/Builders.h" |
| 22 | +#include "mlir/Pass/Pass.h" |
| 23 | +#include "llvm/Support/Regex.h" |
| 24 | + |
| 25 | +namespace mlir { |
| 26 | +namespace gc { |
| 27 | +#define GEN_PASS_DEF_GPUXEVMATTACHTARGET |
| 28 | +#include "gc/Transforms/Passes.h.inc" |
| 29 | +} // namespace gc |
| 30 | +} // namespace mlir |
| 31 | + |
| 32 | +using namespace mlir::xevm; |
| 33 | +using namespace mlir; |
| 34 | + |
| 35 | +namespace { |
| 36 | +struct XeVMAttachTarget |
| 37 | + : public gc::impl::GpuXeVMAttachTargetBase<XeVMAttachTarget> { |
| 38 | + using Base::Base; |
| 39 | + |
| 40 | + // DictionaryAttr getFlags(OpBuilder &builder) const; |
| 41 | + |
| 42 | + void runOnOperation() override; |
| 43 | + |
| 44 | + void getDependentDialects(DialectRegistry ®istry) const override { |
| 45 | + registry.insert<xevm::XeVMDialect>(); |
| 46 | + } |
| 47 | +}; |
| 48 | +} // namespace |
| 49 | + |
| 50 | +void XeVMAttachTarget::runOnOperation() { |
| 51 | + OpBuilder builder(&getContext()); |
| 52 | + auto target = builder.getAttr<XeVMTargetAttr>(optLevel, triple, chip); |
| 53 | + llvm::Regex matcher(moduleMatcher); |
| 54 | + for (Region ®ion : getOperation()->getRegions()) |
| 55 | + for (Block &block : region.getBlocks()) |
| 56 | + for (auto module : block.getOps<gpu::GPUModuleOp>()) { |
| 57 | + // Check if the name of the module matches. |
| 58 | + if (!moduleMatcher.empty() && !matcher.match(module.getName())) |
| 59 | + continue; |
| 60 | + // Create the target array. |
| 61 | + SmallVector<Attribute> targets; |
| 62 | + if (std::optional<ArrayAttr> attrs = module.getTargets()) |
| 63 | + targets.append(attrs->getValue().begin(), attrs->getValue().end()); |
| 64 | + targets.push_back(target); |
| 65 | + // Remove any duplicate targets. |
| 66 | + targets.erase(llvm::unique(targets), targets.end()); |
| 67 | + // Update the target attribute array. |
| 68 | + module.setTargetsAttr(builder.getArrayAttr(targets)); |
| 69 | + } |
| 70 | +} |
0 commit comments