-
Notifications
You must be signed in to change notification settings - Fork 168
[CIR][CIRGen] Implement VisitCXXStdInitializerListExpr to support use of std::initializer_list #764
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3fdc399
implement VisitCXXStdInitializerListExpr
ghehg 528c1b9
add test cases of std::initializer_list
ghehg 972af50
refactoring to move CIR building related functions to CIRGenBuilder.cppp
ghehg f3be1df
more comments, and right formatting
ghehg a295998
finalize change
ghehg 1e49353
rename buildArrayAccessOp, first lettter capitalized in comments
ghehg 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//===-- CIRGenBuilder.cpp - CIRBuilder implementation ---------------------===// | ||
// | ||
// 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 "CIRGenBuilder.h" | ||
|
||
namespace cir { | ||
|
||
mlir::Value CIRGenBuilderTy::maybeBuildArrayDecay(mlir::Location loc, | ||
mlir::Value arrayPtr, | ||
mlir::Type eltTy) { | ||
auto arrayPtrTy = | ||
::mlir::dyn_cast<::mlir::cir::PointerType>(arrayPtr.getType()); | ||
assert(arrayPtrTy && "expected pointer type"); | ||
auto arrayTy = | ||
::mlir::dyn_cast<::mlir::cir::ArrayType>(arrayPtrTy.getPointee()); | ||
|
||
if (arrayTy) { | ||
mlir::cir::PointerType flatPtrTy = | ||
mlir::cir::PointerType::get(getContext(), arrayTy.getEltType()); | ||
return create<mlir::cir::CastOp>( | ||
loc, flatPtrTy, mlir::cir::CastKind::array_to_ptrdecay, arrayPtr); | ||
} | ||
|
||
assert(arrayPtrTy.getPointee() == eltTy && | ||
"flat pointee type must match original array element type"); | ||
return arrayPtr; | ||
} | ||
|
||
mlir::Value CIRGenBuilderTy::getArrayElement(mlir::Location arrayLocBegin, | ||
mlir::Location arrayLocEnd, | ||
mlir::Value arrayPtr, | ||
mlir::Type eltTy, mlir::Value idx, | ||
bool shouldDecay) { | ||
mlir::Value basePtr = arrayPtr; | ||
if (shouldDecay) | ||
basePtr = maybeBuildArrayDecay(arrayLocBegin, arrayPtr, eltTy); | ||
mlir::Type flatPtrTy = basePtr.getType(); | ||
return create<mlir::cir::PtrStrideOp>(arrayLocEnd, flatPtrTy, basePtr, idx); | ||
} | ||
|
||
mlir::cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc, | ||
llvm::APSInt intVal) { | ||
bool isSigned = intVal.isSigned(); | ||
auto width = intVal.getBitWidth(); | ||
mlir::cir::IntType t = isSigned ? getSIntNTy(width) : getUIntNTy(width); | ||
return getConstInt(loc, t, | ||
isSigned ? intVal.getSExtValue() : intVal.getZExtValue()); | ||
} | ||
|
||
mlir::cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc, | ||
llvm::APInt intVal) { | ||
auto width = intVal.getBitWidth(); | ||
mlir::cir::IntType t = getUIntNTy(width); | ||
return getConstInt(loc, t, intVal.getZExtValue()); | ||
} | ||
|
||
mlir::cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc, | ||
mlir::Type t, uint64_t C) { | ||
auto intTy = mlir::dyn_cast<mlir::cir::IntType>(t); | ||
assert(intTy && "expected mlir::cir::IntType"); | ||
return create<mlir::cir::ConstantOp>(loc, intTy, | ||
mlir::cir::IntAttr::get(t, C)); | ||
} | ||
}; // namespace cir |
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
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -fclangir -emit-cir %s -o %t.cir | ||
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s | ||
// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -fclangir -emit-llvm %s -o %t.ll | ||
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM | ||
|
||
namespace std { | ||
template <class b> class initializer_list { | ||
const b *array_start; | ||
const b *array_end; | ||
}; | ||
template <class b> | ||
void f(initializer_list<b>) {;} | ||
void test() { | ||
f({"xy","uv"}); | ||
} | ||
} // namespace std | ||
|
||
// CIR: [[INITLIST_TYPE:!.*]] = !cir.struct<class "std::initializer_list<const char *>" {!cir.ptr<!cir.ptr<!cir.int<s, 8>>>, !cir.ptr<!cir.ptr<!cir.int<s, 8>>>}> | ||
// CIR: cir.func linkonce_odr @_ZSt1fIPKcEvSt16initializer_listIT_E(%arg0: [[INITLIST_TYPE]] | ||
// CIR: [[LOCAL:%.*]] = cir.alloca [[INITLIST_TYPE]], !cir.ptr<[[INITLIST_TYPE]]>, | ||
// CIR: cir.store %arg0, [[LOCAL]] : [[INITLIST_TYPE]], !cir.ptr<[[INITLIST_TYPE]]> | ||
// CIR: cir.return | ||
|
||
// CIR: cir.global "private" constant internal dsolocal [[STR_XY:@.*]] = #cir.const_array<"xy\00" : !cir.array<!s8i x 3>> : !cir.array<!s8i x 3> | ||
// CIR: cir.global "private" constant internal dsolocal [[STR_UV:@.*]] = #cir.const_array<"uv\00" : !cir.array<!s8i x 3>> : !cir.array<!s8i x 3> | ||
|
||
// CIR: cir.func @_ZSt4testv() | ||
// CIR: cir.scope { | ||
// CIR: [[INITLIST_LOCAL:%.*]] = cir.alloca [[INITLIST_TYPE]], !cir.ptr<[[INITLIST_TYPE]]>, | ||
// CIR: [[LOCAL_ELEM_ARRAY:%.*]] = cir.alloca !cir.array<!cir.ptr<!s8i> x 2>, !cir.ptr<!cir.array<!cir.ptr<!s8i> x 2>>, | ||
// CIR: [[FIRST_ELEM_PTR:%.*]] = cir.cast(array_to_ptrdecay, [[LOCAL_ELEM_ARRAY]] : !cir.ptr<!cir.array<!cir.ptr<!s8i> x 2>>), !cir.ptr<!cir.ptr<!s8i>> | ||
// CIR: [[XY_CHAR_ARRAY:%.*]] = cir.get_global [[STR_XY]] : !cir.ptr<!cir.array<!s8i x 3>> | ||
// CIR: [[STR_XY_PTR:%.*]] = cir.cast(array_to_ptrdecay, [[XY_CHAR_ARRAY]] : !cir.ptr<!cir.array<!s8i x 3>>), !cir.ptr<!s8i> | ||
// CIR: cir.store [[STR_XY_PTR]], [[FIRST_ELEM_PTR]] : !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>> | ||
// CIR: [[ONE:%.*]] = cir.const #cir.int<1> | ||
// CIR: [[NEXT_ELEM_PTR:%.*]] = cir.ptr_stride([[FIRST_ELEM_PTR]] : !cir.ptr<!cir.ptr<!s8i>>, [[ONE]] : !s64i), !cir.ptr<!cir.ptr<!s8i>> | ||
// CIR: [[UV_CHAR_ARRAY:%.*]] = cir.get_global [[STR_UV]] : !cir.ptr<!cir.array<!s8i x 3>> | ||
// CIR: [[STR_UV_PTR:%.*]] = cir.cast(array_to_ptrdecay, [[UV_CHAR_ARRAY]] : !cir.ptr<!cir.array<!s8i x 3>>), !cir.ptr<!s8i> | ||
// CIR: cir.store [[STR_UV_PTR]], [[NEXT_ELEM_PTR]] : !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>> | ||
// CIR: [[START_FLD_PTR:%.*]] = cir.get_member [[INITLIST_LOCAL]][0] {name = "array_start"} : !cir.ptr<[[INITLIST_TYPE]]> -> !cir.ptr<!cir.ptr<!cir.ptr<!s8i>>> | ||
// CIR: [[START_FLD_PTR_AS_PTR_2_CHAR_ARRAY:%.*]] = cir.cast(bitcast, [[START_FLD_PTR]] : !cir.ptr<!cir.ptr<!cir.ptr<!s8i>>>), !cir.ptr<!cir.ptr<!cir.array<!cir.ptr<!s8i> x 2>>> | ||
// CIR: cir.store [[LOCAL_ELEM_ARRAY]], [[START_FLD_PTR_AS_PTR_2_CHAR_ARRAY]] : !cir.ptr<!cir.array<!cir.ptr<!s8i> x 2>>, !cir.ptr<!cir.ptr<!cir.array<!cir.ptr<!s8i> x 2>>> | ||
// CIR: [[ELEM_ARRAY_LEN:%.*]] = cir.const #cir.int<2> | ||
// CIR: [[END_FLD_PTR:%.*]] = cir.get_member [[INITLIST_LOCAL]][1] {name = "array_end"} : !cir.ptr<[[INITLIST_TYPE]]> -> !cir.ptr<!cir.ptr<!cir.ptr<!s8i>>> | ||
// CIR: [[LOCAL_ELEM_ARRAY_END:%.*]] = cir.ptr_stride([[LOCAL_ELEM_ARRAY]] : !cir.ptr<!cir.array<!cir.ptr<!s8i> x 2>>, [[ELEM_ARRAY_LEN]] : !u64i), !cir.ptr<!cir.array<!cir.ptr<!s8i> x 2>> | ||
// CIR: [[END_FLD_PTR_AS_PTR_2_CHAR_ARRAY:%.*]] = cir.cast(bitcast, [[END_FLD_PTR]] : !cir.ptr<!cir.ptr<!cir.ptr<!s8i>>>), !cir.ptr<!cir.ptr<!cir.array<!cir.ptr<!s8i> x 2>>> | ||
// CIR: cir.store [[LOCAL_ELEM_ARRAY_END]], [[END_FLD_PTR_AS_PTR_2_CHAR_ARRAY]] : !cir.ptr<!cir.array<!cir.ptr<!s8i> x 2>>, !cir.ptr<!cir.ptr<!cir.array<!cir.ptr<!s8i> x 2>>> | ||
// CIR: [[ARG:%.*]] = cir.load [[INITLIST_LOCAL]] : !cir.ptr<[[INITLIST_TYPE]]>, [[INITLIST_TYPE]] | ||
// CIR: cir.call @_ZSt1fIPKcEvSt16initializer_listIT_E([[ARG]]) : ([[INITLIST_TYPE]]) -> () | ||
// CIR: } | ||
// CIR: cir.return | ||
// CIR: } | ||
|
||
// LLVM: %"class.std::initializer_list<const char *>" = type { ptr, ptr } | ||
|
||
// LLVM: @.str = internal constant [3 x i8] c"xy\00" | ||
// LLVM: @.str1 = internal constant [3 x i8] c"uv\00" | ||
|
||
// LLVM: define linkonce_odr void @_ZSt1fIPKcEvSt16initializer_listIT_E(%"class.std::initializer_list<const char *>" [[ARG0:%.*]]) | ||
// LLVM: [[LOCAL_PTR:%.*]] = alloca %"class.std::initializer_list<const char *>", i64 1, align 8, | ||
// LLVM: store %"class.std::initializer_list<const char *>" [[ARG0]], ptr [[LOCAL_PTR]], align 8, | ||
// LLVM: ret void, | ||
// LLVM: } | ||
|
||
// LLVM: define dso_local void @_ZSt4testv() | ||
// LLVM: br label %[[SCOPE_START:.*]], | ||
// LLVM: [[SCOPE_START]]: ; preds = %0 | ||
// LLVM: [[INIT_STRUCT:%.*]] = alloca %"class.std::initializer_list<const char *>", i64 1, align 8, | ||
// LLVM: [[ELEM_ARRAY_PTR:%.*]] = alloca [2 x ptr], i64 1, align 8, | ||
// LLVM: [[PTR_FIRST_ELEM:%.*]] = getelementptr ptr, ptr [[ELEM_ARRAY_PTR]], i32 0, | ||
// LLVM: store ptr @.str, ptr [[PTR_FIRST_ELEM]], align 8, | ||
// LLVM: [[PTR_SECOND_ELEM:%.*]] = getelementptr ptr, ptr [[PTR_FIRST_ELEM]], i64 1, | ||
// LLVM: store ptr @.str1, ptr [[PTR_SECOND_ELEM]], align 8, | ||
// LLVM: [[INIT_START_FLD_PTR:%.*]] = getelementptr %"class.std::initializer_list<const char *>", ptr [[INIT_STRUCT]], i32 0, i32 0, | ||
// LLVM: [[INIT_END_FLD_PTR:%.*]] = getelementptr %"class.std::initializer_list<const char *>", ptr [[INIT_STRUCT]], i32 0, i32 1, | ||
// LLVM: [[ELEM_ARRAY_END:%.*]] = getelementptr [2 x ptr], ptr [[ELEM_ARRAY_PTR]], i64 2, | ||
// LLVM: store ptr [[ELEM_ARRAY_END]], ptr [[INIT_END_FLD_PTR]], align 8, | ||
// LLVM: [[ARG2PASS:%.*]] = load %"class.std::initializer_list<const char *>", ptr [[INIT_STRUCT]], align 8, | ||
// LLVM: call void @_ZSt1fIPKcEvSt16initializer_listIT_E(%"class.std::initializer_list<const char *>" [[ARG2PASS]]), | ||
// LLVM: br label %[[SCOPE_END:.*]], | ||
// LLVM: [[SCOPE_END]]: ; preds = %[[SCOPE_START]] | ||
// LLVM: ret void |
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.