[Mlir-commits] [flang] [mlir] [mlir][acc] Add OffloadLiveInValueCanonicalization pass (PR #174671)
Razvan Lupusoru
llvmlistbot at llvm.org
Wed Jan 7 09:28:40 PST 2026
================
@@ -0,0 +1,303 @@
+//===- OffloadLiveInValueCanonicalization.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 pass canonicalizes live-in values for regions destined for offloading.
+//
+// Overview:
+// ---------
+// When a region is outlined (extracted into a separate function for device
+// execution), values defined outside the region but used inside become
+// arguments to the outlined function. However, some values cannot be passed
+// as arguments because they represent synthetic types (e.g., shape metadata,
+// field indices) or are better handled by recreating them inside the region.
+//
+// This pass identifies such values and either:
+// 1. Sinks the defining operation into the region (if all uses are inside)
+// 2. Rematerializes (clones) the operation inside the region (if there are
+// uses both inside and outside)
+//
+// Transforms:
+// -----------
+// The pass performs two main transformations on live-in values:
+//
+// 1. Sinking: If a candidate operation's result is only used inside the
+// offload region, the operation is moved into the region.
+//
+// 2. Rematerialization: If a candidate operation's result is used both
+// inside and outside the region, the operation is cloned inside the
+// region and uses within the region are updated to use the clone.
+//
+// Candidate operations are:
+// - Constants (matching arith.constant, etc.)
+// - Operations implementing `acc::OutlineRematerializationOpInterface`
+// - Address-of operations (`acc::AddressOfGlobalOpInterface`) referencing
+// symbols that are valid in GPU regions or constant globals
+//
+// The pass traces through view-like operations (`ViewLikeOpInterface`) and
+// partial entity access operations (`acc::PartialEntityAccessOpInterface`)
+// to find the original defining operation before making candidate decisions.
+//
+// Requirements:
+// -------------
+// To use this pass in a pipeline, the following requirements must be met:
+//
+// 1. Target Region Identification: Operations representing offload regions
+// must implement `acc::OffloadRegionOpInterface`. This interface marks
+// regions that will be outlined for device execution.
+//
+// 2. Rematerialization Candidates: Operations producing values that should
+// be rematerialized (rather than passed as arguments) should implement
+// `acc::OutlineRematerializationOpInterface`. Examples include operations
+// producing shape metadata, field indices, or other synthetic types.
+//
+// 3. Analysis Registration (Optional): If custom behavior is needed for
+// symbol validation (e.g., determining if a global is valid on device),
+// pre-register `acc::OpenACCSupport` analysis on the parent module.
+// If not registered, default behavior will be used.
+//
+// 4. View-Like Operations: Operations that create views or casts should
+// implement `ViewLikeOpInterface` or `acc::PartialEntityAccessOpInterface`
+// to allow the pass to trace through to the original defining operation.
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/OpenACC/Transforms/Passes.h"
+
+#include "mlir/Analysis/TopologicalSortUtils.h"
+#include "mlir/Dialect/Func/IR/FuncOps.h"
+#include "mlir/Dialect/OpenACC/Analysis/OpenACCSupport.h"
+#include "mlir/Dialect/OpenACC/OpenACC.h"
+#include "mlir/IR/Builders.h"
+#include "mlir/IR/Matchers.h"
+#include "mlir/IR/Operation.h"
+#include "mlir/IR/Region.h"
+#include "mlir/IR/SymbolTable.h"
+#include "mlir/IR/Value.h"
+#include "mlir/Interfaces/SideEffectInterfaces.h"
+#include "mlir/Interfaces/ViewLikeInterface.h"
+#include "mlir/Pass/Pass.h"
+#include "mlir/Support/LLVM.h"
+#include "mlir/Transforms/RegionUtils.h"
+
+namespace mlir {
+namespace acc {
+#define GEN_PASS_DEF_OFFLOADLIVEINVALUECANONICALIZATION
+#include "mlir/Dialect/OpenACC/Transforms/Passes.h.inc"
+} // namespace acc
+} // namespace mlir
+
+#define DEBUG_TYPE "offload-livein-value-canonicalization"
+
+using namespace mlir;
+
+namespace {
+
+/// Returns true if all users of the given value are inside the region.
+static bool allUsersAreInsideRegion(Value val, Region ®ion) {
+ for (Operation *user : val.getUsers()) {
+ if (!region.isAncestor(user->getParentRegion()))
+ return false;
+ }
----------------
razvanlupusoru wrote:
Done.
https://github.com/llvm/llvm-project/pull/174671
More information about the Mlir-commits
mailing list