-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[CIR][Upstream] Local initialization for ArrayType #132974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AmrDeveloper
merged 15 commits into
llvm:main
from
AmrDeveloper:cir_upstream_array_local_init
Apr 3, 2025
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4dc1e77
[CIR] [Upstream local initialization for ArrayType
AmrDeveloper ee9c92f
Address code review comments and add getConstantInt
AmrDeveloper d479f0d
Address code review comments
AmrDeveloper 9e10f04
Address code review comments
AmrDeveloper ed0a86b
Address code review comments
AmrDeveloper 56ef35e
Add missing features
AmrDeveloper 7fa7016
Merge branch 'main' into cir_upstream_array_local_init
AmrDeveloper 77a7d5f
Merge remote-tracking branch 'origin/main' into cir_upstream_array_lo…
AmrDeveloper e814e46
Fix merge conflict
AmrDeveloper 50b178f
Remove unused variable
AmrDeveloper 9a9e494
Address code review comments
AmrDeveloper 1609aad
Merge branch 'main' into cir_upstream_array_local_init
AmrDeveloper 8685a63
Merge branch 'main' into cir_upstream_array_local_init
AmrDeveloper 4f1be09
Put emitAggExpr in the correct order
AmrDeveloper 6f29bf8
Merge branch 'main' into cir_upstream_array_local_init
AmrDeveloper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,11 @@ class CIRBaseBuilderTy : public mlir::OpBuilder { | |
| return create<cir::ConstantOp>(loc, attr.getType(), attr); | ||
| } | ||
|
|
||
| // Creates constant null value for integral type ty. | ||
| cir::ConstantOp getNullValue(mlir::Type ty, mlir::Location loc) { | ||
| return create<cir::ConstantOp>(loc, ty, getZeroInitAttr(ty)); | ||
| } | ||
|
|
||
| cir::ConstantOp getBool(bool state, mlir::Location loc) { | ||
| return create<cir::ConstantOp>(loc, getBoolTy(), getCIRBoolAttr(state)); | ||
| } | ||
|
|
@@ -68,6 +73,36 @@ class CIRBaseBuilderTy : public mlir::OpBuilder { | |
| getContext(), mlir::cast<cir::PointerType>(type), valueAttr); | ||
| } | ||
|
|
||
| mlir::TypedAttr getConstNullPtrAttr(mlir::Type t) { | ||
| assert(mlir::isa<cir::PointerType>(t) && "expected cir.ptr"); | ||
| return getConstPtrAttr(t, 0); | ||
| } | ||
|
|
||
| mlir::TypedAttr getZeroAttr(mlir::Type t) { | ||
| return cir::ZeroAttr::get(getContext(), t); | ||
| } | ||
|
|
||
| mlir::TypedAttr getZeroInitAttr(mlir::Type ty) { | ||
| if (mlir::isa<cir::IntType>(ty)) | ||
| return cir::IntAttr::get(ty, 0); | ||
| if (auto fltType = mlir::dyn_cast<cir::SingleType>(ty)) | ||
|
||
| return cir::FPAttr::getZero(fltType); | ||
| if (auto fltType = mlir::dyn_cast<cir::DoubleType>(ty)) | ||
| return cir::FPAttr::getZero(fltType); | ||
| if (auto fltType = mlir::dyn_cast<cir::FP16Type>(ty)) | ||
| return cir::FPAttr::getZero(fltType); | ||
| if (auto fltType = mlir::dyn_cast<cir::BF16Type>(ty)) | ||
| return cir::FPAttr::getZero(fltType); | ||
| if (auto arrTy = mlir::dyn_cast<cir::ArrayType>(ty)) | ||
| return getZeroAttr(arrTy); | ||
| if (auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty)) | ||
| return getConstNullPtrAttr(ptrTy); | ||
| if (mlir::isa<cir::BoolType>(ty)) { | ||
| return getCIRBoolAttr(false); | ||
| } | ||
| llvm_unreachable("Zero initializer for given type is NYI"); | ||
| } | ||
|
|
||
| mlir::Value createAlloca(mlir::Location loc, cir::PointerType addrType, | ||
| mlir::Type type, llvm::StringRef name, | ||
| mlir::IntegerAttr alignment) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -79,6 +79,13 @@ class LLVMLoweringInfo { | |||||
| class CIR_Op<string mnemonic, list<Trait> traits = []> : | ||||||
| Op<CIR_Dialect, mnemonic, traits>, LLVMLoweringInfo; | ||||||
|
|
||||||
| //===----------------------------------------------------------------------===// | ||||||
| // CIR Op Traits | ||||||
| //===----------------------------------------------------------------------===// | ||||||
|
|
||||||
| def SameFirstOperandAndResultType : | ||||||
| NativeOpTrait<"SameFirstOperandAndResultType">; | ||||||
|
|
||||||
| //===----------------------------------------------------------------------===// | ||||||
| // CastOp | ||||||
| //===----------------------------------------------------------------------===// | ||||||
|
|
@@ -229,6 +236,43 @@ def CastOp : CIR_Op<"cast", | |||||
| let hasFolder = 1; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| //===----------------------------------------------------------------------===// | ||||||
| // PtrStrideOp | ||||||
| //===----------------------------------------------------------------------===// | ||||||
|
|
||||||
| def PtrStrideOp : CIR_Op<"ptr_stride", | ||||||
| [Pure, SameFirstOperandAndResultType]> { | ||||||
| let summary = "Pointer access with stride"; | ||||||
| let description = [{ | ||||||
| Given a base pointer as first operand, provides a new pointer after applying | ||||||
| a stride (second operand). | ||||||
|
|
||||||
| ```mlir | ||||||
| %3 = cir.const 0 : i32 | ||||||
| %4 = cir.ptr_stride(%2 : !cir.ptr<i32>, %3 : i32), !cir.ptr<i32> | ||||||
| ``` | ||||||
| }]; | ||||||
|
|
||||||
| let arguments = (ins CIR_PointerType:$base, PrimitiveInt:$stride); | ||||||
| let results = (outs CIR_PointerType:$result); | ||||||
|
|
||||||
| let assemblyFormat = [{ | ||||||
| `(` $base `:` qualified(type($base)) `,` $stride `:` qualified(type($stride)) `)` | ||||||
bcardosolopes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| `,` qualified(type($result)) attr-dict | ||||||
| }]; | ||||||
|
|
||||||
| let extraClassDeclaration = [{ | ||||||
| // Get type pointed by the base pointer. | ||||||
| mlir::Type getElementTy() { | ||||||
| return mlir::cast<cir::PointerType>(getBase().getType()).getPointee(); | ||||||
| } | ||||||
| }]; | ||||||
|
|
||||||
| // SameFirstOperandAndResultType already checks all we need. | ||||||
| let hasVerifier = 0; | ||||||
|
||||||
| // SameFirstOperandAndResultType already checks all we need. | |
| let hasVerifier = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| //====- LoweringHelpers.h - Lowering helper functions ---------------------===// | ||
| // | ||
| // 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 declares helper functions for lowering from CIR to LLVM or MLIR. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| #ifndef LLVM_CLANG_CIR_LOWERINGHELPERS_H | ||
| #define LLVM_CLANG_CIR_LOWERINGHELPERS_H | ||
|
|
||
| #include "mlir/IR/BuiltinAttributes.h" | ||
| #include "mlir/Transforms/DialectConversion.h" | ||
| #include "clang/CIR/Dialect/IR/CIRDialect.h" | ||
|
|
||
| mlir::DenseElementsAttr | ||
| convertStringAttrToDenseElementsAttr(cir::ConstArrayAttr attr, mlir::Type type); | ||
|
|
||
| template <typename StorageTy> StorageTy getZeroInitFromType(mlir::Type ty); | ||
| template <> mlir::APInt getZeroInitFromType(mlir::Type ty); | ||
| template <> mlir::APFloat getZeroInitFromType(mlir::Type ty); | ||
|
|
||
| template <typename AttrTy, typename StorageTy> | ||
| void convertToDenseElementsAttrImpl(cir::ConstArrayAttr attr, | ||
| llvm::SmallVectorImpl<StorageTy> &values); | ||
|
|
||
| template <typename AttrTy, typename StorageTy> | ||
| mlir::DenseElementsAttr | ||
| convertToDenseElementsAttr(cir::ConstArrayAttr attr, | ||
| const llvm::SmallVectorImpl<int64_t> &dims, | ||
| mlir::Type type); | ||
|
|
||
| std::optional<mlir::Attribute> | ||
| lowerConstArrayAttr(cir::ConstArrayAttr constArr, | ||
| const mlir::TypeConverter *converter); | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.