[clang] [mlir] [CIR] Add CIRABIRewriteContext for ABI function/call rewriting (PR #192119)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Fri May 1 14:50:03 PDT 2026
================
@@ -0,0 +1,469 @@
+//===- CIRABIRewriteContext.cpp - CIR-specific ABI rewriting --------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "CIRABIRewriteContext.h"
+#include "mlir/IR/Builders.h"
+#include "mlir/IR/Types.h"
+#include "clang/CIR/Dialect/IR/CIRAttrs.h"
+#include "clang/CIR/Dialect/IR/CIRDialect.h"
+#include "clang/CIR/Dialect/IR/CIROpsEnums.h"
+
+using namespace cir;
+using namespace mlir;
+using namespace mlir::abi;
+
+/// Emit a value coercion between two types. For scalar-to-scalar
+/// (e.g. integer sign extension), a direct cir.cast is sufficient.
+/// When one of the types is a record (struct), LLVM IR's bitcast
+/// cannot reinterpret between aggregate and scalar types, so we go
+/// through memory: alloca srcTy -> store src -> bitcast ptr -> load
+/// dstTy.
+static Value emitCoercion(OpBuilder &rewriter, Location loc, Type dstTy,
+ Value src) {
+ Type srcTy = src.getType();
+ if (srcTy == dstTy)
+ return src;
+
+ bool needsMemory =
+ mlir::isa<cir::RecordType, cir::ComplexType>(srcTy) ||
+ mlir::isa<cir::RecordType, cir::ComplexType>(dstTy) ||
+ (mlir::isa<cir::VectorType>(srcTy) != mlir::isa<cir::VectorType>(dstTy));
+
+ if (!needsMemory)
+ return cir::CastOp::create(rewriter, loc, dstTy, cir::CastKind::bitcast,
+ src);
+
+ auto srcPtrTy = cir::PointerType::get(srcTy);
+ auto dstPtrTy = cir::PointerType::get(dstTy);
+
+ auto alloca =
+ cir::AllocaOp::create(rewriter, loc, srcPtrTy, srcTy,
+ /*name=*/rewriter.getStringAttr("coerce"),
+ /*alignment=*/rewriter.getI64IntegerAttr(8));
----------------
andykaylor wrote:
The alignment here seems quite arbitrary.
https://github.com/llvm/llvm-project/pull/192119
More information about the cfe-commits
mailing list