Skip to content

Commit 3507f62

Browse files
Add the pinned attribute to the alloca op
1 parent 83552a2 commit 3507f62

File tree

8 files changed

+93
-25
lines changed

8 files changed

+93
-25
lines changed

flang/include/flang/Optimizer/Builder/FIRBuilder.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ class FirOpBuilder : public mlir::OpBuilder {
116116
}
117117

118118
/// Create a slot for a local on the stack. Besides the variable's type and
119-
/// shape, it may be given name or target attributes.
119+
/// shape, it may be given name, pinned, or target attributes.
120+
mlir::Value allocateLocal(mlir::Location loc, mlir::Type ty,
121+
llvm::StringRef uniqName, llvm::StringRef name,
122+
bool pinned, llvm::ArrayRef<mlir::Value> shape,
123+
llvm::ArrayRef<mlir::Value> lenParams,
124+
bool asTarget = false);
120125
mlir::Value allocateLocal(mlir::Location loc, mlir::Type ty,
121126
llvm::StringRef uniqName, llvm::StringRef name,
122127
llvm::ArrayRef<mlir::Value> shape,

flang/include/flang/Optimizer/Dialect/FIROps.td

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def fir_AllocaOp : fir_Op<"alloca", [AttrSizedOperandSegments,
129129
TypeAttr:$in_type,
130130
OptionalAttr<StrAttr>:$uniq_name,
131131
OptionalAttr<StrAttr>:$bindc_name,
132+
UnitAttr:$pinned,
132133
Variadic<AnyIntegerType>:$typeparams,
133134
Variadic<AnyIntegerType>:$shape
134135
);
@@ -144,10 +145,23 @@ def fir_AllocaOp : fir_Op<"alloca", [AttrSizedOperandSegments,
144145
CArg<"mlir::ValueRange", "{}">:$shape,
145146
CArg<"llvm::ArrayRef<mlir::NamedAttribute>", "{}">:$attributes)>,
146147
OpBuilder<(ins "mlir::Type":$inType, "llvm::StringRef":$uniqName,
148+
"llvm::StringRef":$bindcName, "bool":$pinned,
147149
CArg<"mlir::ValueRange", "{}">:$typeparams,
148150
CArg<"mlir::ValueRange", "{}">:$shape,
149151
CArg<"llvm::ArrayRef<mlir::NamedAttribute>", "{}">:$attributes)>,
150-
OpBuilder<(ins "mlir::Type":$in_type,
152+
OpBuilder<(ins "mlir::Type":$inType, "llvm::StringRef":$uniqName,
153+
CArg<"mlir::ValueRange", "{}">:$typeparams,
154+
CArg<"mlir::ValueRange", "{}">:$shape,
155+
CArg<"llvm::ArrayRef<mlir::NamedAttribute>", "{}">:$attributes)>,
156+
OpBuilder<(ins "mlir::Type":$inType, "llvm::StringRef":$uniqName,
157+
"bool":$pinned, CArg<"mlir::ValueRange", "{}">:$typeparams,
158+
CArg<"mlir::ValueRange", "{}">:$shape,
159+
CArg<"llvm::ArrayRef<mlir::NamedAttribute>", "{}">:$attributes)>,
160+
OpBuilder<(ins "mlir::Type":$inType, "bool":$pinned,
161+
CArg<"mlir::ValueRange", "{}">:$typeparams,
162+
CArg<"mlir::ValueRange", "{}">:$shape,
163+
CArg<"llvm::ArrayRef<mlir::NamedAttribute>", "{}">:$attributes)>,
164+
OpBuilder<(ins "mlir::Type":$inType,
151165
CArg<"mlir::ValueRange", "{}">:$typeparams,
152166
CArg<"mlir::ValueRange", "{}">:$shape,
153167
CArg<"llvm::ArrayRef<mlir::NamedAttribute>", "{}">:$attributes)>];

flang/lib/Lower/Bridge.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
334334
mlir::Type symType = genType(sym);
335335
return builder->allocateLocal(
336336
loc, symType, mangleName(sym), toStringRef(sym.GetUltimate().name()),
337-
shape, typeParams,
337+
/*pinned=*/true, shape, typeParams,
338338
sym.GetUltimate().attrs().test(Fortran::semantics::Attr::TARGET));
339339
};
340340

flang/lib/Optimizer/Builder/FIRBuilder.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ elideLengthsAlreadyInType(mlir::Type type, mlir::ValueRange lenParams) {
151151
/// A local variable ought to have a name in the source code.
152152
mlir::Value fir::FirOpBuilder::allocateLocal(
153153
mlir::Location loc, mlir::Type ty, llvm::StringRef uniqName,
154-
llvm::StringRef name, llvm::ArrayRef<mlir::Value> shape,
154+
llvm::StringRef name, bool pinned, llvm::ArrayRef<mlir::Value> shape,
155155
llvm::ArrayRef<mlir::Value> lenParams, bool asTarget) {
156156
// Convert the shape extents to `index`, as needed.
157157
llvm::SmallVector<mlir::Value> indices;
@@ -172,14 +172,23 @@ mlir::Value fir::FirOpBuilder::allocateLocal(
172172
// Create the local variable.
173173
if (name.empty()) {
174174
if (uniqName.empty())
175-
return create<fir::AllocaOp>(loc, ty, elidedLenParams, indices, attrs);
176-
return create<fir::AllocaOp>(loc, ty, uniqName, elidedLenParams, indices,
177-
attrs);
175+
return create<fir::AllocaOp>(loc, ty, pinned, elidedLenParams, indices,
176+
attrs);
177+
return create<fir::AllocaOp>(loc, ty, uniqName, pinned, elidedLenParams,
178+
indices, attrs);
178179
}
179-
return create<fir::AllocaOp>(loc, ty, uniqName, name, elidedLenParams,
180+
return create<fir::AllocaOp>(loc, ty, uniqName, name, pinned, elidedLenParams,
180181
indices, attrs);
181182
}
182183

184+
mlir::Value fir::FirOpBuilder::allocateLocal(
185+
mlir::Location loc, mlir::Type ty, llvm::StringRef uniqName,
186+
llvm::StringRef name, llvm::ArrayRef<mlir::Value> shape,
187+
llvm::ArrayRef<mlir::Value> lenParams, bool asTarget) {
188+
return allocateLocal(loc, ty, uniqName, name, /*pinned=*/false, shape,
189+
lenParams, asTarget);
190+
}
191+
183192
/// Get the block for adding Allocas.
184193
mlir::Block *fir::FirOpBuilder::getAllocaBlock() {
185194
auto iface =
@@ -205,9 +214,13 @@ fir::FirOpBuilder::createTemporary(mlir::Location loc, mlir::Type type,
205214
setInsertionPointToStart(getAllocaBlock());
206215
}
207216

217+
// If the alloca is inside an OpenMP Op which will be outlined then pin the
218+
// alloca here.
219+
const bool pinned =
220+
getRegion().getParentOfType<mlir::omp::OutlineableOpenMPOpInterface>();
208221
assert(!type.isa<fir::ReferenceType>() && "cannot be a reference");
209222
auto ae = create<fir::AllocaOp>(loc, type, /*unique_name=*/llvm::StringRef{},
210-
name, dynamicLength, dynamicShape, attrs);
223+
name, pinned, dynamicLength, dynamicShape, attrs);
211224
if (hoistAlloc)
212225
restoreInsertionPoint(insPt);
213226
return ae;

flang/lib/Optimizer/Dialect/FIROps.cpp

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,18 @@ void fir::AllocaOp::build(mlir::OpBuilder &builder,
170170
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
171171
auto nameAttr = builder.getStringAttr(uniqName);
172172
build(builder, result, wrapAllocaResultType(inType), inType, nameAttr, {},
173-
typeparams, shape);
173+
/*pinned=*/false, typeparams, shape);
174+
result.addAttributes(attributes);
175+
}
176+
177+
void fir::AllocaOp::build(mlir::OpBuilder &builder,
178+
mlir::OperationState &result, mlir::Type inType,
179+
llvm::StringRef uniqName, bool pinned,
180+
mlir::ValueRange typeparams, mlir::ValueRange shape,
181+
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
182+
auto nameAttr = builder.getStringAttr(uniqName);
183+
build(builder, result, wrapAllocaResultType(inType), inType, nameAttr, {},
184+
pinned, typeparams, shape);
174185
result.addAttributes(attributes);
175186
}
176187

@@ -184,7 +195,22 @@ void fir::AllocaOp::build(mlir::OpBuilder &builder,
184195
auto bindcAttr =
185196
bindcName.empty() ? mlir::StringAttr{} : builder.getStringAttr(bindcName);
186197
build(builder, result, wrapAllocaResultType(inType), inType, nameAttr,
187-
bindcAttr, typeparams, shape);
198+
bindcAttr, /*pinned=*/false, typeparams, shape);
199+
result.addAttributes(attributes);
200+
}
201+
202+
void fir::AllocaOp::build(mlir::OpBuilder &builder,
203+
mlir::OperationState &result, mlir::Type inType,
204+
llvm::StringRef uniqName, llvm::StringRef bindcName,
205+
bool pinned, mlir::ValueRange typeparams,
206+
mlir::ValueRange shape,
207+
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
208+
auto nameAttr =
209+
uniqName.empty() ? mlir::StringAttr{} : builder.getStringAttr(uniqName);
210+
auto bindcAttr = bindcName.empty() ? mlir::StringAttr{}
211+
: builder.getStringAttr(bindcName);
212+
build(builder, result, wrapAllocaResultType(inType), inType, nameAttr,
213+
bindcAttr, pinned, typeparams, shape);
188214
result.addAttributes(attributes);
189215
}
190216

@@ -193,6 +219,16 @@ void fir::AllocaOp::build(mlir::OpBuilder &builder,
193219
mlir::ValueRange typeparams, mlir::ValueRange shape,
194220
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
195221
build(builder, result, wrapAllocaResultType(inType), inType, {}, {},
222+
/*pinned=*/false, typeparams, shape);
223+
result.addAttributes(attributes);
224+
}
225+
226+
void fir::AllocaOp::build(mlir::OpBuilder &builder,
227+
mlir::OperationState &result, mlir::Type inType,
228+
bool pinned, mlir::ValueRange typeparams,
229+
mlir::ValueRange shape,
230+
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
231+
build(builder, result, wrapAllocaResultType(inType), inType, {}, {}, pinned,
196232
typeparams, shape);
197233
result.addAttributes(attributes);
198234
}

flang/test/Lower/OpenMP/omp-check-privatise-params.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
!FIRDialect-DAG: %[[N_CVT1:.*]] = fir.convert %[[N]] : (i32) -> i64
99
!FIRDialect-DAG: %[[N_CVT2:.*]] = fir.convert %[[N_CVT1]] : (i64) -> index
1010
!FIRDialect-DAG: omp.parallel {
11-
!FIRDialect-DAG: {{.*}} = fir.alloca !fir.array<?xi32>, %[[N_CVT2]] {{{.*}}, uniq_name = "_QFarrayEx"}
11+
!FIRDialect-DAG: {{.*}} = fir.alloca !fir.array<?xi32>, %[[N_CVT2]] {{{.*}}, pinned, uniq_name = "_QFarrayEx"}
1212
!FIRDialect: omp.terminator
1313

1414
subroutine array(x,n)

flang/test/Lower/OpenMP/omp-parallel-private-clause.f90

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
!FIRDialect-DAG: %[[BETA_ARRAY:.*]] = fir.alloca !fir.array<10x!fir.char<1,5>> {{{.*}}, uniq_name = "{{.*}}Ebeta_array"}
1212

1313
!FIRDialect-DAG: omp.parallel {
14-
!FIRDialect-DAG: %[[ALPHA_PRIVATE:.*]] = fir.alloca i32 {{{.*}}, uniq_name = "{{.*}}Ealpha"}
15-
!FIRDialect-DAG: %[[ALPHA_ARRAY_PRIVATE:.*]] = fir.alloca !fir.array<10xi32> {{{.*}}, uniq_name = "{{.*}}Ealpha_array"}
16-
!FIRDialect-DAG: %[[BETA_PRIVATE:.*]] = fir.alloca !fir.char<1,5> {{{.*}}, uniq_name = "{{.*}}Ebeta"}
17-
!FIRDialect-DAG: %[[BETA_ARRAY_PRIVATE:.*]] = fir.alloca !fir.array<10x!fir.char<1,5>> {{{.*}}, uniq_name = "{{.*}}Ebeta_array"}
18-
!FIRDialect-DAG: %[[ARG1_PRIVATE:.*]] = fir.alloca i32 {{{.*}}, uniq_name = "{{.*}}Earg1"}
19-
!FIRDialect-DAG: %[[ARG2_ARRAY_PRIVATE:.*]] = fir.alloca !fir.array<10xi32> {{{.*}}, uniq_name = "{{.*}}Earg2"}
20-
!FIRDialect-DAG: %[[ARG3_PRIVATE:.*]] = fir.alloca !fir.char<1,5> {{{.*}}, uniq_name = "{{.*}}Earg3"}
21-
!FIRDialect-DAG: %[[ARG4_ARRAY_PRIVATE:.*]] = fir.alloca !fir.array<10x!fir.char<1,5>> {{{.*}}, uniq_name = "{{.*}}Earg4"}
14+
!FIRDialect-DAG: %[[ALPHA_PRIVATE:.*]] = fir.alloca i32 {{{.*}}, pinned, uniq_name = "{{.*}}Ealpha"}
15+
!FIRDialect-DAG: %[[ALPHA_ARRAY_PRIVATE:.*]] = fir.alloca !fir.array<10xi32> {{{.*}}, pinned, uniq_name = "{{.*}}Ealpha_array"}
16+
!FIRDialect-DAG: %[[BETA_PRIVATE:.*]] = fir.alloca !fir.char<1,5> {{{.*}}, pinned, uniq_name = "{{.*}}Ebeta"}
17+
!FIRDialect-DAG: %[[BETA_ARRAY_PRIVATE:.*]] = fir.alloca !fir.array<10x!fir.char<1,5>> {{{.*}}, pinned, uniq_name = "{{.*}}Ebeta_array"}
18+
!FIRDialect-DAG: %[[ARG1_PRIVATE:.*]] = fir.alloca i32 {{{.*}}, pinned, uniq_name = "{{.*}}Earg1"}
19+
!FIRDialect-DAG: %[[ARG2_ARRAY_PRIVATE:.*]] = fir.alloca !fir.array<10xi32> {{{.*}}, pinned, uniq_name = "{{.*}}Earg2"}
20+
!FIRDialect-DAG: %[[ARG3_PRIVATE:.*]] = fir.alloca !fir.char<1,5> {{{.*}}, pinned, uniq_name = "{{.*}}Earg3"}
21+
!FIRDialect-DAG: %[[ARG4_ARRAY_PRIVATE:.*]] = fir.alloca !fir.array<10x!fir.char<1,5>> {{{.*}}, pinned, uniq_name = "{{.*}}Earg4"}
2222
!FIRDialect: omp.terminator
2323
!FIRDialect: }
2424

flang/test/Lower/OpenMP/omp-seqloop.f90

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ program wsloop
99

1010
!FIRDialect: omp.parallel {
1111
!$OMP PARALLEL
12-
!FIRDialect: %[[PRIVATE_INDX:.*]] = fir.alloca i32 {{{.*}}bindc_name = "i"}
12+
!FIRDialect: %[[PRIVATE_INDX:.*]] = fir.alloca i32 {bindc_name = "i", pinned}
1313
!FIRDialect: %[[FINAL_INDX:.*]] = fir.do_loop %[[INDX:.*]] = {{.*}} {
1414
do i=1, 9
1515
print*, i
@@ -28,7 +28,7 @@ subroutine sub1
2828
integer :: i
2929
integer :: arr(10)
3030
!FIRDialect: omp.parallel {
31-
!FIRDialect: {{.*}} = fir.alloca i32 {bindc_name = "i"}
31+
!FIRDialect: {{.*}} = fir.alloca i32 {bindc_name = "i", pinned}
3232
!$OMP PARALLEL
3333
do i=1, 10
3434
arr(i) = i
@@ -44,7 +44,7 @@ subroutine sub2
4444
integer :: i
4545
integer :: arr(10)
4646
!FIRDialect: omp.parallel {
47-
!FIRDialect: {{.*}} = fir.alloca i32 {bindc_name = "i"}
47+
!FIRDialect: {{.*}} = fir.alloca i32 {bindc_name = "i", pinned}
4848
!$OMP PARALLEL
4949
!FIRDialect: omp.master {
5050
!$OMP MASTER
@@ -67,15 +67,15 @@ subroutine sub3
6767
integer :: i,j
6868
integer :: arr(10)
6969
!FIRDialect: omp.parallel {
70-
!FIRDialect: {{.*}} = fir.alloca i32 {bindc_name = "i"}
70+
!FIRDialect: {{.*}} = fir.alloca i32 {bindc_name = "i", pinned}
7171
!$OMP PARALLEL
7272
do i=1, 10
7373
arr(i) = i
7474
end do
7575
!FIRDialect: omp.master {
7676
!$OMP MASTER
7777
!FIRDialect: omp.parallel {
78-
!FIRDialect: {{.*}} = fir.alloca i32 {bindc_name = "j"}
78+
!FIRDialect: {{.*}} = fir.alloca i32 {bindc_name = "j", pinned}
7979
!$OMP PARALLEL
8080
do j=1, 10
8181
arr(j) = j

0 commit comments

Comments
 (0)