================
@@ -0,0 +1,77 @@
+//===- 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
+
+void TargetLoweringPass::runOnOp(cir::LoadOp op,
+                                 cir::LowerModule &lowerModule) {
+  if (std::optional<cir::SyncScopeKind> syncScope = op.getSyncScope())
+    op.setSyncScope(
+        lowerModule.getTargetLoweringInfo().convertSyncScope(*syncScope));
+}
+
+void TargetLoweringPass::runOnOp(cir::StoreOp op,
+                                 cir::LowerModule &lowerModule) {
+  if (std::optional<cir::SyncScopeKind> syncScope = op.getSyncScope())
+    op.setSyncScope(
+        lowerModule.getTargetLoweringInfo().convertSyncScope(*syncScope));
+}
+
+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)
+    return;
+
+  llvm::SmallVector<mlir::Operation *> opsToTransform;
+  mod->walk([&](mlir::Operation *op) {
+    if (mlir::isa<cir::LoadOp, cir::StoreOp>(op))
+      opsToTransform.push_back(op);
+  });
+
+  for (mlir::Operation *op : opsToTransform) {
+    mlir::TypeSwitch<mlir::Operation *>(op)
+        .Case<cir::LoadOp>([&](auto load) { runOnOp(load, *lowerModule); })
+        .Case<cir::StoreOp>([&](auto store) { runOnOp(store, *lowerModule); });
+  }
----------------
xlauko wrote:

No need to collect ops since you are not making structureal IR changes here. 
This should be enough:

```cpp
  mod->walk([&](mlir::Operation *op) {
    mlir::TypeSwitch<mlir::Operation *>(op)
        .Case<cir::LoadOp>([&](auto load) { runOnOp(load, *lowerModule); })
        .Case<cir::StoreOp>([&](auto store) { runOnOp(store, *lowerModule); });
  });
```

https://github.com/llvm/llvm-project/pull/179245
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to