[llvm-branch-commits] [llvm] [LoongArch] Add memory barrier optimization pass (PR #218597)

Nikita Popov via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Sep 15 01:57:12 PDT 2026


================
@@ -0,0 +1,561 @@
+//===---- LoongArchMemoryBarrierOpt.cpp - Memory barrier Optimization -----===//
+//
+// 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 pass removes or merges redundant memory barrier instructions.
+///
+/// - DBAR x + DBAR y -> DBAR (x & y)
+/// - DBAR x + AMO_DB -> AMO_DB
+/// - DBAR x + AMO    -> AMO_DB
+/// - DBAR x + LL     -> LL
+/// - AMO_DB + DBAR x -> AMO_DB
+/// - AMO    + DBAR x -> AMO_DB
+/// - SC     + DBAR x -> SC
+///
+//===----------------------------------------------------------------------===//
+
+#include "LoongArch.h"
+#include "LoongArchInstrInfo.h"
+#include "LoongArchSubtarget.h"
+#include "llvm/CodeGen/MachineDominators.h"
+#include "llvm/CodeGen/MachinePostDominators.h"
+#include "llvm/InitializePasses.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "loongarch-memory-barrier-opt"
+#define LOONGARCH_MEMORY_BARRIER_OPT_NAME                                      \
+  "LoongArch Memory Barrier Optimisation pass"
+
+static cl::opt<bool> RequireNoPathBypass(
+    "loongarch-require-no-path-bypass",
+    cl::desc("Optimize only when no paths bypass either memory barrier"),
+    cl::init(true), cl::Hidden);
+
+static cl::opt<bool> MergeAMOWithMB(
+    "loongarch-merge-amo-with-dbar",
+    cl::desc("Merge AMOs with DBARs into AMO_DB during optimization"),
+    cl::init(true), cl::Hidden);
+
+static cl::opt<bool> DisableInlineAsm(
+    "loongarch-disable-inline-asm-barrier-opt",
+    cl::desc("Disable optimization of memory barriers in InlineAsm"),
+    cl::init(false), cl::Hidden);
----------------
nikic wrote:

Of course just dropping it directly without reverting would also be fine, if that's simpler.

For the kernel use case, would it be viable for the kernel to switch to builtins instead, if those exist?

https://github.com/llvm/llvm-project/pull/218597


More information about the llvm-branch-commits mailing list