Skip to content

Commit e423dfb

Browse files
committed
[Flang][OpenMP] Reapply revert of llvm#155742 with fix for Nekbone
Small addition to the FunctionFiltering pass to handle the new storage argument for the HLFIR declare operation.
1 parent d286e16 commit e423dfb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+559
-280
lines changed

flang/include/flang/Lower/AbstractConverter.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ class AbstractConverter {
108108
/// added or replaced at the inner-most level of the local symbol map.
109109
virtual void bindSymbol(SymbolRef sym, const fir::ExtendedValue &exval) = 0;
110110

111+
/// Binds the symbol's physical storage to a storage descriptor,
112+
/// which is the base address of the storage and the offset
113+
/// within the storage, where the symbol begins.
114+
/// The symbol binding will be added or replaced at the innermost level
115+
/// of the local symbol map.
116+
virtual void
117+
bindSymbolStorage(SymbolRef sym,
118+
Fortran::lower::SymMap::StorageDesc storage) = 0;
119+
120+
/// Returns the storage descriptor previously bound to this symbol.
121+
/// If there is no bound storage, the descriptor will contain
122+
/// nullptr base address.
123+
virtual Fortran::lower::SymMap::StorageDesc
124+
getSymbolStorage(SymbolRef sym) = 0;
125+
111126
/// Override lowering of expression with pre-lowered values.
112127
/// Associate mlir::Value to evaluate::Expr. All subsequent call to
113128
/// genExprXXX() will replace any occurrence of an overridden

flang/include/flang/Lower/ConvertVariable.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,14 @@ void defineCommonBlocks(
9292
/// The COMMON block is a global structure. \p commonValue is the base address
9393
/// of the COMMON block. As the offset from the symbol \p sym, generate the
9494
/// COMMON block member value (commonValue + offset) for the symbol.
95+
/// \p commonSize specifies the syze of the COMMON block in bytes.
96+
/// The size is used to represent a COMMON block reference as
97+
/// a !fir.ref<!fir.array<SIZExi8>>.
9598
mlir::Value genCommonBlockMember(AbstractConverter &converter,
9699
mlir::Location loc,
97100
const Fortran::semantics::Symbol &sym,
98-
mlir::Value commonValue);
101+
mlir::Value commonValue,
102+
std::size_t commonSize);
99103

100104
/// Lower a symbol attributes given an optional storage \p and add it to the
101105
/// provided symbol map. If \preAlloc is not provided, a temporary storage will

flang/include/flang/Lower/SymbolMap.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,22 @@ struct SymbolBox : public fir::details::matcher<SymbolBox> {
146146
class SymMap {
147147
public:
148148
using AcDoVar = llvm::StringRef;
149+
/// Descriptor of a symbol's storage consists of the base address
150+
/// of the storage and the offset within that storage.
151+
using StorageDesc = std::pair<mlir::Value, std::uint64_t>;
149152

150153
SymMap() { pushScope(); }
151154
SymMap(const SymMap &) = delete;
152155

153-
void pushScope() { symbolMapStack.emplace_back(); }
156+
void pushScope() {
157+
symbolMapStack.emplace_back();
158+
storageMapStack.emplace_back();
159+
}
154160
void popScope() {
155161
symbolMapStack.pop_back();
156162
assert(symbolMapStack.size() >= 1);
163+
storageMapStack.pop_back();
164+
assert(storageMapStack.size() >= 1);
157165
}
158166

159167
/// Add an extended value to the symbol table.
@@ -287,6 +295,8 @@ class SymMap {
287295
symbolMapStack.emplace_back();
288296
assert(symbolMapStack.size() == 1);
289297
impliedDoStack.clear();
298+
storageMapStack.clear();
299+
storageMapStack.emplace_back();
290300
}
291301

292302
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
@@ -315,6 +325,16 @@ class SymMap {
315325
return std::nullopt;
316326
}
317327

328+
/// Register the symbol's storage at the innermost level
329+
/// of the symbol table. If the storage is already registered,
330+
/// it will be replaced.
331+
void registerStorage(semantics::SymbolRef sym, StorageDesc storage);
332+
/// Lookup the symbol's storage at the innermost level of the symbol table.
333+
StorageDesc lookupStorage(semantics::SymbolRef sym);
334+
StorageDesc lookupStorage(const semantics::Symbol *sym) {
335+
return lookupStorage(*sym);
336+
}
337+
318338
private:
319339
/// Bind `box` to `symRef` in the symbol map.
320340
void makeSym(semantics::SymbolRef symRef, const SymbolBox &box,
@@ -332,6 +352,10 @@ class SymMap {
332352
// Implied DO induction variables are not represented as Se::Symbol in
333353
// Ev::Expr. Keep the variable markers in their own stack.
334354
llvm::SmallVector<std::pair<AcDoVar, mlir::Value>> impliedDoStack;
355+
356+
// A stack of maps between the symbols and their storage descriptors.
357+
llvm::SmallVector<llvm::DenseMap<const semantics::Symbol *, StorageDesc>>
358+
storageMapStack;
335359
};
336360

337361
/// RAII wrapper for SymMap.

flang/include/flang/Optimizer/Builder/HLFIRTools.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ fir::FortranVariableOpInterface
224224
genDeclare(mlir::Location loc, fir::FirOpBuilder &builder,
225225
const fir::ExtendedValue &exv, llvm::StringRef name,
226226
fir::FortranVariableFlagsAttr flags,
227-
mlir::Value dummyScope = nullptr,
227+
mlir::Value dummyScope = nullptr, mlir::Value storage = nullptr,
228+
std::uint64_t storageOffset = 0,
228229
cuf::DataAttributeAttr dataAttr = {});
229230

230231
/// Generate an hlfir.associate to build a variable from an expression value.

flang/include/flang/Optimizer/HLFIR/HLFIROps.td

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,12 @@ def hlfir_DeclareOp
109109
attr-dict `:` functional-type(operands, results)
110110
}];
111111

112-
let builders = [
113-
OpBuilder<(ins "mlir::Value":$memref, "llvm::StringRef":$uniq_name,
114-
CArg<"mlir::Value", "{}">:$shape, CArg<"mlir::ValueRange", "{}">:$typeparams,
112+
let builders = [OpBuilder<(ins "mlir::Value":$memref,
113+
"llvm::StringRef":$uniq_name, CArg<"mlir::Value", "{}">:$shape,
114+
CArg<"mlir::ValueRange", "{}">:$typeparams,
115115
CArg<"mlir::Value", "{}">:$dummy_scope,
116+
CArg<"mlir::Value", "{}">:$storage,
117+
CArg<"std::uint64_t", "0">:$storage_offset,
116118
CArg<"fir::FortranVariableFlagsAttr", "{}">:$fortran_attrs,
117119
CArg<"cuf::DataAttributeAttr", "{}">:$data_attr)>];
118120

flang/lib/Lower/Bridge.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,17 @@ class FirConverter : public Fortran::lower::AbstractConverter {
632632
addSymbol(sym, exval, /*forced=*/true);
633633
}
634634

635+
void bindSymbolStorage(
636+
Fortran::lower::SymbolRef sym,
637+
Fortran::lower::SymMap::StorageDesc storage) override final {
638+
localSymbols.registerStorage(sym, std::move(storage));
639+
}
640+
641+
Fortran::lower::SymMap::StorageDesc
642+
getSymbolStorage(Fortran::lower::SymbolRef sym) override final {
643+
return localSymbols.lookupStorage(sym);
644+
}
645+
635646
void
636647
overrideExprValues(const Fortran::lower::ExprToValueMap *map) override final {
637648
exprValueOverrides = map;

flang/lib/Lower/ConvertArrayConstructor.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,8 @@ class RuntimeTempStrategy : public StrategyBase {
315315
mlir::Value tempStorage = builder.createHeapTemporary(
316316
loc, declaredType, tempName, extents, lengths);
317317
mlir::Value shape = builder.genShape(loc, extents);
318-
declare = hlfir::DeclareOp::create(
319-
builder, loc, tempStorage, tempName, shape, lengths,
320-
/*dummy_scope=*/nullptr, fir::FortranVariableFlagsAttr{});
318+
declare = hlfir::DeclareOp::create(builder, loc, tempStorage, tempName,
319+
shape, lengths);
321320
initialBoxValue =
322321
builder.createBox(loc, boxType, declare->getOriginalBase(), shape,
323322
/*slice=*/mlir::Value{}, lengths, /*tdesc=*/{});

flang/lib/Lower/ConvertExprToHLFIR.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,10 +1813,8 @@ class HlfirBuilder {
18131813
// Allocate scalar temporary that will be initialized
18141814
// with the values specified by the constructor.
18151815
mlir::Value storagePtr = builder.createTemporary(loc, recTy);
1816-
auto varOp = hlfir::EntityWithAttributes{hlfir::DeclareOp::create(
1817-
builder, loc, storagePtr, "ctor.temp", /*shape=*/nullptr,
1818-
/*typeparams=*/mlir::ValueRange{}, /*dummy_scope=*/nullptr,
1819-
fir::FortranVariableFlagsAttr{})};
1816+
auto varOp = hlfir::EntityWithAttributes{
1817+
hlfir::DeclareOp::create(builder, loc, storagePtr, "ctor.temp")};
18201818

18211819
// Initialize any components that need initialization.
18221820
mlir::Value box = builder.createBox(loc, fir::ExtendedValue{varOp});

flang/lib/Lower/ConvertVariable.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,7 @@ static void instantiateAlias(Fortran::lower::AbstractConverter &converter,
13551355
mlir::Value bytePtr = fir::CoordinateOp::create(
13561356
builder, loc, i8Ptr, storeAddr, mlir::ValueRange{offset});
13571357
mlir::Value typedPtr = castAliasToPointer(builder, loc, symType, bytePtr);
1358+
converter.bindSymbolStorage(sym, {storeAddr, off});
13581359
Fortran::lower::StatementContext stmtCtx;
13591360
mapSymbolAttributes(converter, var, symMap, stmtCtx, typedPtr);
13601361
// Default initialization is possible for equivalence members: see
@@ -1594,13 +1595,15 @@ void Fortran::lower::defineCommonBlocks(
15941595

15951596
mlir::Value Fortran::lower::genCommonBlockMember(
15961597
Fortran::lower::AbstractConverter &converter, mlir::Location loc,
1597-
const Fortran::semantics::Symbol &sym, mlir::Value commonValue) {
1598+
const Fortran::semantics::Symbol &sym, mlir::Value commonValue,
1599+
std::size_t commonSize) {
15981600
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
15991601

16001602
std::size_t byteOffset = sym.GetUltimate().offset();
16011603
mlir::IntegerType i8Ty = builder.getIntegerType(8);
16021604
mlir::Type i8Ptr = builder.getRefType(i8Ty);
1603-
mlir::Type seqTy = builder.getRefType(builder.getVarLenSeqTy(i8Ty));
1605+
fir::SequenceType::Shape shape(1, commonSize);
1606+
mlir::Type seqTy = builder.getRefType(fir::SequenceType::get(shape, i8Ty));
16041607
mlir::Value base = builder.createConvert(loc, seqTy, commonValue);
16051608

16061609
mlir::Value offs =
@@ -1609,6 +1612,8 @@ mlir::Value Fortran::lower::genCommonBlockMember(
16091612
mlir::ValueRange{offs});
16101613
mlir::Type symType = converter.genType(sym);
16111614

1615+
converter.bindSymbolStorage(sym, {base, byteOffset});
1616+
16121617
return Fortran::semantics::FindEquivalenceSet(sym) != nullptr
16131618
? castAliasToPointer(builder, loc, symType, varAddr)
16141619
: builder.createConvert(loc, builder.getRefType(symType), varAddr);
@@ -1637,7 +1642,8 @@ static void instantiateCommon(Fortran::lower::AbstractConverter &converter,
16371642
symMap.addSymbol(common, commonAddr);
16381643
}
16391644

1640-
mlir::Value local = genCommonBlockMember(converter, loc, varSym, commonAddr);
1645+
mlir::Value local =
1646+
genCommonBlockMember(converter, loc, varSym, commonAddr, common.size());
16411647
Fortran::lower::StatementContext stmtCtx;
16421648
mapSymbolAttributes(converter, var, symMap, stmtCtx, local);
16431649
}
@@ -1909,7 +1915,8 @@ static void genDeclareSymbol(Fortran::lower::AbstractConverter &converter,
19091915
// Declare a local pointer variable.
19101916
auto newBase = hlfir::DeclareOp::create(
19111917
builder, loc, boxAlloc, name, /*shape=*/nullptr, lenParams,
1912-
/*dummy_scope=*/nullptr, attributes);
1918+
/*dummy_scope=*/nullptr, /*storage=*/nullptr,
1919+
/*storage_offset=*/0, attributes);
19131920
mlir::Value nullAddr = builder.createNullConstant(
19141921
loc, llvm::cast<fir::BaseBoxType>(ptrBoxType).getEleTy());
19151922

@@ -1939,9 +1946,10 @@ static void genDeclareSymbol(Fortran::lower::AbstractConverter &converter,
19391946
mlir::Value dummyScope;
19401947
if (converter.isRegisteredDummySymbol(sym))
19411948
dummyScope = converter.dummyArgsScopeValue();
1942-
auto newBase =
1943-
hlfir::DeclareOp::create(builder, loc, base, name, shapeOrShift,
1944-
lenParams, dummyScope, attributes, dataAttr);
1949+
auto [storage, storageOffset] = converter.getSymbolStorage(sym);
1950+
auto newBase = hlfir::DeclareOp::create(
1951+
builder, loc, base, name, shapeOrShift, lenParams, dummyScope, storage,
1952+
storageOffset, attributes, dataAttr);
19451953
symMap.addVariableDefinition(sym, newBase, force);
19461954
return;
19471955
}
@@ -1999,8 +2007,10 @@ void Fortran::lower::genDeclareSymbol(
19992007
base = genPackArray(converter, sym, exv);
20002008
dummyScope = converter.dummyArgsScopeValue();
20012009
}
2002-
hlfir::EntityWithAttributes declare = hlfir::genDeclare(
2003-
loc, builder, base, name, attributes, dummyScope, dataAttr);
2010+
auto [storage, storageOffset] = converter.getSymbolStorage(sym);
2011+
hlfir::EntityWithAttributes declare =
2012+
hlfir::genDeclare(loc, builder, base, name, attributes, dummyScope,
2013+
storage, storageOffset, dataAttr);
20042014
symMap.addVariableDefinition(sym, declare.getIfVariableInterface(), force);
20052015
return;
20062016
}

flang/lib/Lower/OpenACC.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,12 +1270,10 @@ mlir::acc::FirstprivateRecipeOp Fortran::lower::createOrGetFirstprivateRecipe(
12701270

12711271
auto leftDeclOp = hlfir::DeclareOp::create(
12721272
builder, loc, recipe.getCopyRegion().getArgument(0), llvm::StringRef{},
1273-
shape, llvm::ArrayRef<mlir::Value>{}, /*dummy_scope=*/nullptr,
1274-
fir::FortranVariableFlagsAttr{});
1273+
shape);
12751274
auto rightDeclOp = hlfir::DeclareOp::create(
12761275
builder, loc, recipe.getCopyRegion().getArgument(1), llvm::StringRef{},
1277-
shape, llvm::ArrayRef<mlir::Value>{}, /*dummy_scope=*/nullptr,
1278-
fir::FortranVariableFlagsAttr{});
1276+
shape);
12791277

12801278
hlfir::DesignateOp::Subscripts triplets =
12811279
getSubscriptsFromArgs(recipe.getCopyRegion().getArguments());
@@ -1590,14 +1588,10 @@ static void genCombiner(fir::FirOpBuilder &builder, mlir::Location loc,
15901588
auto shape =
15911589
genShapeFromBoundsOrArgs(loc, builder, seqTy, bounds,
15921590
recipe.getCombinerRegion().getArguments());
1593-
auto v1DeclareOp = hlfir::DeclareOp::create(
1594-
builder, loc, value1, llvm::StringRef{}, shape,
1595-
llvm::ArrayRef<mlir::Value>{},
1596-
/*dummy_scope=*/nullptr, fir::FortranVariableFlagsAttr{});
1597-
auto v2DeclareOp = hlfir::DeclareOp::create(
1598-
builder, loc, value2, llvm::StringRef{}, shape,
1599-
llvm::ArrayRef<mlir::Value>{},
1600-
/*dummy_scope=*/nullptr, fir::FortranVariableFlagsAttr{});
1591+
auto v1DeclareOp = hlfir::DeclareOp::create(builder, loc, value1,
1592+
llvm::StringRef{}, shape);
1593+
auto v2DeclareOp = hlfir::DeclareOp::create(builder, loc, value2,
1594+
llvm::StringRef{}, shape);
16011595
hlfir::DesignateOp::Subscripts triplets = getTripletsFromArgs(recipe);
16021596

16031597
llvm::SmallVector<mlir::Value> lenParamsLeft;

0 commit comments

Comments
 (0)