[clang] [CIR] Add TargetLowering pass (PR #179245)
Sirui Mu via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 4 07:05:22 PST 2026
================
@@ -0,0 +1,80 @@
+//===- TargetLowering.cpp -------------------------------------------------===//
+//
+// Part of the LLVM Project, 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
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the cir-target-lowering pass.
+//
+//===----------------------------------------------------------------------===//
+
+#include "TargetLowering/LowerModule.h"
+
+#include "mlir/Support/LLVM.h"
+#include "clang/CIR/Dialect/Passes.h"
+#include "llvm/ADT/TypeSwitch.h"
+
+using namespace mlir;
+using namespace cir;
+
+namespace mlir {
+#define GEN_PASS_DEF_TARGETLOWERING
+#include "clang/CIR/Dialect/Passes.h.inc"
+} // namespace mlir
+
+namespace {
+
+struct TargetLoweringPass
+ : public impl::TargetLoweringBase<TargetLoweringPass> {
+ TargetLoweringPass() = default;
+ void runOnOperation() override;
+
+private:
+ void runOnOp(cir::LoadOp op, cir::LowerModule &lowerModule);
+ void runOnOp(cir::StoreOp op, cir::LowerModule &lowerModule);
+};
+
+} // namespace
+
+static void convertSyncScopeIfPresent(mlir::Operation *op,
+ cir::LowerModule &lowerModule) {
+ auto syncScopeAttr =
+ mlir::cast_if_present<cir::SyncScopeKindAttr>(op->getAttr("sync_scope"));
+ if (syncScopeAttr) {
+ cir::SyncScopeKind convertedSyncScope =
+ lowerModule.getTargetLoweringInfo().convertSyncScope(
+ syncScopeAttr.getValue());
+ op->setAttr("sync_scope", cir::SyncScopeKindAttr::get(op->getContext(),
+ convertedSyncScope));
+ }
+}
+
+void TargetLoweringPass::runOnOp(cir::LoadOp op,
+ cir::LowerModule &lowerModule) {
+ convertSyncScopeIfPresent(op, lowerModule);
+}
+
+void TargetLoweringPass::runOnOp(cir::StoreOp op,
+ cir::LowerModule &lowerModule) {
+ convertSyncScopeIfPresent(op, lowerModule);
+}
+
+void TargetLoweringPass::runOnOperation() {
+ auto mod = mlir::cast<mlir::ModuleOp>(getOperation());
+ auto lowerModule = cir::createLowerModule(mod);
+ // If lower module is not available, skip the target lowering pass.
+ if (!lowerModule)
----------------
Lancern wrote:
Updated.
The `CXXABILowering` pass also has a fragment of similar code. It is updated, too.
https://github.com/llvm/llvm-project/pull/179245
More information about the cfe-commits
mailing list