Skip to content

Commit 15abba4

Browse files
committed
[CIR][Lower][MLIR] Handle pointer decay of higher dimensions arrays
1 parent 90b70f4 commit 15abba4

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
@@ -45,6 +45,7 @@
4545
#include "mlir/IR/ValueRange.h"
4646
#include "mlir/Pass/Pass.h"
4747
#include "mlir/Pass/PassManager.h"
48+
#include "mlir/Support/LLVM.h"
4849
#include "mlir/Support/LogicalResult.h"
4950
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
5051
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
@@ -118,6 +119,17 @@ mlir::Type lowerArrayType(cir::ArrayType type, bool hasValueSemantics,
118119
return convertToReferenceType(shape, elementType);
119120
}
120121

122+
// Compute the identity stride for the default layout of a memref
123+
static llvm::SmallVector<std::int64_t> identityStrides(mlir::MemRefType t) {
124+
llvm::SmallVector<std::int64_t> strides(t.getShape().size());
125+
if (!strides.empty())
126+
strides.back() = 1;
127+
// To replace by range algorithms with an exclusive scan...
128+
for (auto i = strides.size(); i > 1; --i)
129+
strides[i - 2] = t.getShape()[i - 1] * strides[i - 1];
130+
return strides;
131+
}
132+
121133
class CIRReturnLowering : public mlir::OpConversionPattern<cir::ReturnOp> {
122134
public:
123135
using OpConversionPattern<cir::ReturnOp>::OpConversionPattern;
@@ -171,7 +183,7 @@ class CIRCallOpLowering : public mlir::OpConversionPattern<cir::CallOp> {
171183
};
172184

173185
/// Emits the value from memory as expected by its users. Should be called when
174-
/// the memory represetnation of a CIR type is not equal to its scalar
186+
/// the memory representation of a CIR type is not equal to its scalar
175187
/// representation.
176188
static mlir::Value emitFromMemory(mlir::ConversionPatternRewriter &rewriter,
177189
cir::LoadOp op, mlir::Value value) {
@@ -1176,7 +1188,8 @@ class CIRCastOpLowering : public mlir::OpConversionPattern<cir::CastOp> {
11761188
case CIR::array_to_ptrdecay: {
11771189
auto newDstType = mlir::cast<mlir::MemRefType>(convertTy(dstType));
11781190
rewriter.replaceOpWithNewOp<mlir::memref::ReinterpretCastOp>(
1179-
op, newDstType, src, 0, std::nullopt, std::nullopt);
1191+
op, newDstType, src, 0, newDstType.getShape(),
1192+
identityStrides(newDstType));
11801193
return mlir::success();
11811194
}
11821195
case CIR::bitcast: {
@@ -1334,7 +1347,7 @@ class CIRPtrStrideOpLowering
13341347
// memref.reinterpret_cast (%base, %stride)
13351348
//
13361349
// MemRef Dialect doesn't have GEP-like operation. memref.reinterpret_cast
1337-
// only been used to propogate %base and %stride to memref.load/store and
1350+
// only been used to propagate %base and %stride to memref.load/store and
13381351
// should be erased after the conversion.
13391352
mlir::LogicalResult
13401353
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)