Skip to content

Commit 262b9b5

Browse files
authored
[CIR][Upstream] Local initialization for ArrayType (#132974)
This change adds local initialization for ArrayType Issue #130197
1 parent 7514225 commit 262b9b5

25 files changed

+1064
-21
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

+15
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
6767
return create<cir::ConstantOp>(loc, attr.getType(), attr);
6868
}
6969

70+
cir::ConstantOp getConstantInt(mlir::Location loc, mlir::Type ty,
71+
int64_t value) {
72+
return getConstant(loc, cir::IntAttr::get(ty, value));
73+
}
74+
75+
// Creates constant null value for integral type ty.
76+
cir::ConstantOp getNullValue(mlir::Type ty, mlir::Location loc) {
77+
return getConstant(loc, getZeroInitAttr(ty));
78+
}
79+
7080
mlir::TypedAttr getConstNullPtrAttr(mlir::Type t) {
7181
assert(mlir::isa<cir::PointerType>(t) && "expected cir.ptr");
7282
return getConstPtrAttr(t, 0);
@@ -171,6 +181,11 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
171181
return createLoad(loc, addr);
172182
}
173183

184+
cir::PtrStrideOp createPtrStride(mlir::Location loc, mlir::Value base,
185+
mlir::Value stride) {
186+
return create<cir::PtrStrideOp>(loc, base.getType(), base, stride);
187+
}
188+
174189
//===--------------------------------------------------------------------===//
175190
// Cast/Conversion Operators
176191
//===--------------------------------------------------------------------===//

clang/include/clang/CIR/Dialect/IR/CIRDialect.h

+25
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,31 @@
3232
#include "clang/CIR/Interfaces/CIRLoopOpInterface.h"
3333
#include "clang/CIR/Interfaces/CIROpInterfaces.h"
3434

35+
namespace mlir {
36+
namespace OpTrait {
37+
38+
namespace impl {
39+
// These functions are out-of-line implementations of the methods in the
40+
// corresponding trait classes. This avoids them being template
41+
// instantiated/duplicated.
42+
LogicalResult verifySameFirstOperandAndResultType(Operation *op);
43+
} // namespace impl
44+
45+
/// This class provides verification for ops that are known to have the same
46+
/// first operand and result type.
47+
///
48+
template <typename ConcreteType>
49+
class SameFirstOperandAndResultType
50+
: public TraitBase<ConcreteType, SameFirstOperandAndResultType> {
51+
public:
52+
static llvm::LogicalResult verifyTrait(Operation *op) {
53+
return impl::verifySameFirstOperandAndResultType(op);
54+
}
55+
};
56+
57+
} // namespace OpTrait
58+
} // namespace mlir
59+
3560
using BuilderCallbackRef =
3661
llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)>;
3762

clang/include/clang/CIR/Dialect/IR/CIROps.td

+41
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ class LLVMLoweringInfo {
7979
class CIR_Op<string mnemonic, list<Trait> traits = []> :
8080
Op<CIR_Dialect, mnemonic, traits>, LLVMLoweringInfo;
8181

82+
//===----------------------------------------------------------------------===//
83+
// CIR Op Traits
84+
//===----------------------------------------------------------------------===//
85+
86+
def SameFirstOperandAndResultType :
87+
NativeOpTrait<"SameFirstOperandAndResultType">;
88+
8289
//===----------------------------------------------------------------------===//
8390
// CastOp
8491
//===----------------------------------------------------------------------===//
@@ -229,6 +236,40 @@ def CastOp : CIR_Op<"cast",
229236
let hasFolder = 1;
230237
}
231238

239+
240+
//===----------------------------------------------------------------------===//
241+
// PtrStrideOp
242+
//===----------------------------------------------------------------------===//
243+
244+
def PtrStrideOp : CIR_Op<"ptr_stride",
245+
[Pure, SameFirstOperandAndResultType]> {
246+
let summary = "Pointer access with stride";
247+
let description = [{
248+
Given a base pointer as first operand, provides a new pointer after applying
249+
a stride (second operand).
250+
251+
```mlir
252+
%3 = cir.const 0 : i32
253+
%4 = cir.ptr_stride(%2 : !cir.ptr<i32>, %3 : i32), !cir.ptr<i32>
254+
```
255+
}];
256+
257+
let arguments = (ins CIR_PointerType:$base, PrimitiveInt:$stride);
258+
let results = (outs CIR_PointerType:$result);
259+
260+
let assemblyFormat = [{
261+
`(` $base `:` qualified(type($base)) `,` $stride `:`
262+
qualified(type($stride)) `)` `,` qualified(type($result)) attr-dict
263+
}];
264+
265+
let extraClassDeclaration = [{
266+
// Get type pointed by the base pointer.
267+
mlir::Type getElementTy() {
268+
return mlir::cast<cir::PointerType>(getBase().getType()).getPointee();
269+
}
270+
}];
271+
}
272+
232273
//===----------------------------------------------------------------------===//
233274
// ConstantOp
234275
//===----------------------------------------------------------------------===//
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//====- LoweringHelpers.h - Lowering helper functions ---------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file declares helper functions for lowering from CIR to LLVM or MLIR.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
#ifndef LLVM_CLANG_CIR_LOWERINGHELPERS_H
13+
#define LLVM_CLANG_CIR_LOWERINGHELPERS_H
14+
15+
#include "mlir/IR/BuiltinAttributes.h"
16+
#include "mlir/Transforms/DialectConversion.h"
17+
#include "clang/CIR/Dialect/IR/CIRDialect.h"
18+
19+
mlir::DenseElementsAttr
20+
convertStringAttrToDenseElementsAttr(cir::ConstArrayAttr attr, mlir::Type type);
21+
22+
template <typename StorageTy> StorageTy getZeroInitFromType(mlir::Type ty);
23+
template <> mlir::APInt getZeroInitFromType(mlir::Type ty);
24+
template <> mlir::APFloat getZeroInitFromType(mlir::Type ty);
25+
26+
template <typename AttrTy, typename StorageTy>
27+
void convertToDenseElementsAttrImpl(cir::ConstArrayAttr attr,
28+
llvm::SmallVectorImpl<StorageTy> &values);
29+
30+
template <typename AttrTy, typename StorageTy>
31+
mlir::DenseElementsAttr
32+
convertToDenseElementsAttr(cir::ConstArrayAttr attr,
33+
const llvm::SmallVectorImpl<int64_t> &dims,
34+
mlir::Type type);
35+
36+
std::optional<mlir::Attribute>
37+
lowerConstArrayAttr(cir::ConstArrayAttr constArr,
38+
const mlir::TypeConverter *converter);
39+
40+
#endif

clang/include/clang/CIR/MissingFeatures.h

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ struct MissingFeatures {
118118
static bool vectorType() { return false; }
119119
static bool complexType() { return false; }
120120
static bool fixedPointType() { return false; }
121+
static bool stringTypeWithDifferentArraySize() { return false; }
121122

122123
// Future CIR operations
123124
static bool awaitOp() { return false; }

clang/lib/CIR/CodeGen/Address.h

+10
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,23 @@ class Address {
7070
return pointerAndKnownNonNull.getPointer();
7171
}
7272

73+
mlir::Type getType() const {
74+
assert(mlir::cast<cir::PointerType>(
75+
pointerAndKnownNonNull.getPointer().getType())
76+
.getPointee() == elementType);
77+
78+
return mlir::cast<cir::PointerType>(getPointer().getType());
79+
}
80+
7381
mlir::Type getElementType() const {
7482
assert(isValid());
7583
assert(mlir::cast<cir::PointerType>(
7684
pointerAndKnownNonNull.getPointer().getType())
7785
.getPointee() == elementType);
7886
return elementType;
7987
}
88+
89+
clang::CharUnits getAlignment() const { return alignment; }
8090
};
8191

8292
} // namespace clang::CIRGen

clang/lib/CIR/CodeGen/CIRGenDecl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ void CIRGenFunction::emitExprAsInit(const Expr *init, const ValueDecl *d,
251251
return;
252252
}
253253
case cir::TEK_Aggregate:
254-
cgm.errorNYI(init->getSourceRange(), "emitExprAsInit: aggregate type");
254+
emitAggExpr(init, AggValueSlot::forLValue(lvalue));
255255
return;
256256
}
257257
llvm_unreachable("bad evaluation kind");

0 commit comments

Comments
 (0)