[Mlir-commits] [mlir] [mlir][acc] Add utilities for converting acc.loop to scf (PR #172953)

Razvan Lupusoru llvmlistbot at llvm.org
Mon Dec 22 10:12:49 PST 2025


================
@@ -0,0 +1,313 @@
+//===- OpenACCUtilsLoop.cpp - OpenACC Loop Utilities ----------------------===//
+//
+// 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 contains utility functions for converting OpenACC loops to SCF.
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/OpenACC/OpenACCUtilsLoop.h"
+
+#include "mlir/Dialect/Arith/IR/Arith.h"
+#include "mlir/Dialect/Arith/Utils/Utils.h"
+#include "mlir/Dialect/OpenACC/OpenACC.h"
+#include "mlir/Dialect/SCF/IR/SCF.h"
+#include "mlir/Dialect/SCF/Utils/Utils.h"
+#include "mlir/IR/IRMapping.h"
+
+using namespace mlir;
+
+namespace {
+
+/// Calculate trip count for a loop: (ub - lb + step) / step
+/// If inclusiveUpperbound is false, subtracts 1 from ub first.
+static Value calculateTripCount(OpBuilder &b, Location loc, Value lb, Value ub,
+                                Value step, bool inclusiveUpperbound) {
+  Type type = b.getIndexType();
+
+  // Convert original loop arguments to index type
+  lb = getValueOrCreateCastToIndexLike(b, loc, type, lb);
+  ub = getValueOrCreateCastToIndexLike(b, loc, type, ub);
+  step = getValueOrCreateCastToIndexLike(b, loc, type, step);
+
+  if (!inclusiveUpperbound) {
+    Value one = arith::ConstantIndexOp::create(b, loc, 1);
+    ub = b.createOrFold<arith::SubIOp>(loc, ub, one);
----------------
razvanlupusoru wrote:

Done.

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


More information about the Mlir-commits mailing list