Skip to content

Commit 82efd42

Browse files
committed
[CIR][Lower][MLIR] Handle pointer decay of higher dimensions arrays
1 parent 6f33dc2 commit 82efd42

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "mlir/IR/ValueRange.h"
4949
#include "mlir/Pass/Pass.h"
5050
#include "mlir/Pass/PassManager.h"
51+
#include "mlir/Support/LLVM.h"
5152
#include "mlir/Support/LogicalResult.h"
5253
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
5354
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
@@ -121,6 +122,17 @@ mlir::Type lowerArrayType(cir::ArrayType type, bool hasValueSemantics,
121122
return convertToReferenceType(shape, elementType);
122123
}
123124

125+
// Compute the identity stride for the default layout of a memref
126+
static llvm::SmallVector<std::int64_t> identityStrides(mlir::MemRefType t) {
127+
llvm::SmallVector<std::int64_t> strides(t.getShape().size());
128+
if (!strides.empty())
129+
strides.back() = 1;
130+
// To replace by range algorithms with an exclusive scan...
131+
for (auto i = strides.size(); i > 1; --i)
132+
strides[i - 2] = t.getShape()[i - 1] * strides[i - 1];
133+
return strides;
134+
}
135+
124136
class CIRReturnLowering : public mlir::OpConversionPattern<cir::ReturnOp> {
125137
public:
126138
using OpConversionPattern<cir::ReturnOp>::OpConversionPattern;
@@ -174,7 +186,7 @@ class CIRCallOpLowering : public mlir::OpConversionPattern<cir::CallOp> {
174186
};
175187

176188
/// Emits the value from memory as expected by its users. Should be called when
177-
/// the memory represetnation of a CIR type is not equal to its scalar
189+
/// the memory representation of a CIR type is not equal to its scalar
178190
/// representation.
179191
static mlir::Value emitFromMemory(mlir::ConversionPatternRewriter &rewriter,
180192
cir::LoadOp op, mlir::Value value) {
@@ -1289,7 +1301,8 @@ class CIRCastOpLowering : public mlir::OpConversionPattern<cir::CastOp> {
12891301
case CIR::array_to_ptrdecay: {
12901302
auto newDstType = mlir::cast<mlir::MemRefType>(convertTy(dstType));
12911303
rewriter.replaceOpWithNewOp<mlir::memref::ReinterpretCastOp>(
1292-
op, newDstType, src, 0, std::nullopt, std::nullopt);
1304+
op, newDstType, src, 0, newDstType.getShape(),
1305+
identityStrides(newDstType));
12931306
return mlir::success();
12941307
}
12951308
case CIR::bitcast: {
@@ -1447,7 +1460,7 @@ class CIRPtrStrideOpLowering
14471460
// memref.reinterpret_cast (%base, %stride)
14481461
//
14491462
// MemRef Dialect doesn't have GEP-like operation. memref.reinterpret_cast
1450-
// only been used to propogate %base and %stride to memref.load/store and
1463+
// only been used to propagate %base and %stride to memref.load/store and
14511464
// should be erased after the conversion.
14521465
mlir::LogicalResult
14531466
matchAndRewrite(cir::PtrStrideOp op, OpAdaptor adaptor,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir %s -o %t.mlir
2+
// RUN: FileCheck --input-file=%t.mlir %s
3+
4+
5+
int main() {
6+
int a[10];
7+
int b[4][7];
8+
int *aa = a;
9+
int *p = &a[0];
10+
auto *ap = &a;
11+
*ap[3] = 7;
12+
auto *ap10 = (int (*)[10]) p;
13+
14+
auto v15 = b[1][5];
15+
int *bpd = b[0];
16+
auto pb36 = &b[3][6];
17+
auto pb2 = &b[2];
18+
auto pb2a = b[2];
19+
20+
return a[3];
21+
22+
// CHECK: %[[ALLOCA:.+]] = memref.alloca() {alignment = 8 : i64} : memref<!named_tuple.named_tuple<"s", [i32, f64, i8, tensor<5xf32>]>>
23+
// CHECK: %[[C_7:.+]] = arith.constant 7 : i32/home/rkeryell/Xilinx/Projects/LLVM/worktrees/clangir/build/bin/clang -cc1 -internal-isystem /home/rkeryell/Xilinx/Projects/LLVM/worktrees/clangir/build/lib/clang/20/include -nostdsysteminc -triple x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir /home/rkeryell/Xilinx/Projects/LLVM/worktrees/clangir/clang/test/CIR/Lowering/ThroughMLIR/cast.cpp -o
24+
// CHECK: %[[I8_EQUIV_A:.+]] = named_tuple.cast %[[ALLOCA]] : memref<!named_tuple.named_tuple<"s", [i32, f64, i8, tensor<5xf32>]>> to memref<40xi8>
25+
// CHECK: %[[OFFSET_A:.+]] = arith.constant 0 : index
26+
// CHECK: %[[VIEW_A:.+]] = memref.view %[[I8_EQUIV_A]][%[[OFFSET_A]]][] : memref<40xi8> to memref<i32>
27+
// CHECK: memref.store %[[C_7]], %[[VIEW_A]][] : memref<i32>
28+
}

mlir/utils/emacs/mlir-lsp-client.el

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
;;; mlir-lsp-clinet.el --- LSP clinet for the MLIR.
1+
;;; mlir-lsp-client.el --- LSP clinet for the MLIR.
22

33
;; Copyright (C) 2022 The MLIR Authors.
44
;;
@@ -18,7 +18,7 @@
1818

1919
;;; Commentary:
2020

21-
;; LSP clinet to use with `mlir-mode' that uses `mlir-lsp-server' or any
21+
;; LSP client to use with `mlir-mode' that uses `mlir-lsp-server' or any
2222
;; user made compatible server.
2323

2424
;;; Code:

0 commit comments

Comments
 (0)