clang 23.0.0git
TargetLowering.cpp
Go to the documentation of this file.
1//===- TargetLowering.cpp -------------------------------------------------===//
2//
3// Part of the LLVM Project, 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 cir-target-lowering pass.
10//
11//===----------------------------------------------------------------------===//
12
14
15#include "mlir/Support/LLVM.h"
17#include "llvm/ADT/TypeSwitch.h"
18
19using namespace mlir;
20using namespace cir;
21
22namespace mlir {
23#define GEN_PASS_DEF_TARGETLOWERING
24#include "clang/CIR/Dialect/Passes.h.inc"
25} // namespace mlir
26
27namespace {
28
29struct TargetLoweringPass
30 : public impl::TargetLoweringBase<TargetLoweringPass> {
31 TargetLoweringPass() = default;
32 void runOnOperation() override;
33};
34
35} // namespace
36
37static void convertSyncScopeIfPresent(mlir::Operation *op,
38 cir::LowerModule &lowerModule) {
39 auto syncScopeAttr =
40 mlir::cast_if_present<cir::SyncScopeKindAttr>(op->getAttr("sync_scope"));
41 if (syncScopeAttr) {
42 cir::SyncScopeKind convertedSyncScope =
44 syncScopeAttr.getValue());
45 op->setAttr("sync_scope", cir::SyncScopeKindAttr::get(op->getContext(),
46 convertedSyncScope));
47 }
48}
49
50void TargetLoweringPass::runOnOperation() {
51 auto mod = mlir::cast<mlir::ModuleOp>(getOperation());
52 std::unique_ptr<cir::LowerModule> lowerModule = cir::createLowerModule(mod);
53 // If lower module is not available, skip the target lowering pass.
54 if (!lowerModule) {
55 mod.emitWarning("Cannot create a CIR lower module, skipping the ")
56 << getName() << " pass";
57 return;
58 }
59
60 mod->walk([&](mlir::Operation *op) {
61 if (mlir::isa<cir::LoadOp, cir::StoreOp, cir::AtomicXchgOp,
62 cir::AtomicCmpXchgOp, cir::AtomicFetchOp>(op))
63 convertSyncScopeIfPresent(op, *lowerModule);
64 });
65}
66
67std::unique_ptr<Pass> mlir::createTargetLoweringPass() {
68 return std::make_unique<TargetLoweringPass>();
69}
static void convertSyncScopeIfPresent(mlir::Operation *op, cir::LowerModule &lowerModule)
const TargetLoweringInfo & getTargetLoweringInfo()
virtual cir::SyncScopeKind convertSyncScope(cir::SyncScopeKind syncScope) const
std::unique_ptr< LowerModule > createLowerModule(mlir::ModuleOp module)
StringRef getName(const HeaderType T)
Definition HeaderFile.h:38
std::unique_ptr< Pass > createTargetLoweringPass()