Skip to content

Commit

Permalink
[flang][NFC] move loadIfRef to FIRBuilder (#84306)
Browse files Browse the repository at this point in the history
This will be useful for OpenMP too.

I changed the definition slightly to use `fir::isa_ref_type` (which also
includes llvm pointers) because I think it reads better using the common
type helpers. There shouldn't be any llvm pointers in lowering so this
isn't a functional change.
  • Loading branch information
tblah committed Mar 8, 2024
1 parent a110a1c commit 860a400
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
4 changes: 4 additions & 0 deletions flang/include/flang/Optimizer/Builder/FIRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ class FirOpBuilder : public mlir::OpBuilder, public mlir::OpBuilder::Listener {
void createStoreWithConvert(mlir::Location loc, mlir::Value val,
mlir::Value addr);

/// Create a fir.load if \p val is a reference or pointer type. Return the
/// result of the load if it was created, otherwise return \p val
mlir::Value loadIfRef(mlir::Location loc, mlir::Value val);

/// Create a new FuncOp. If the function may have already been created, use
/// `addNamedFunction` instead.
mlir::func::FuncOp createFunction(mlir::Location loc, llvm::StringRef name,
Expand Down
12 changes: 2 additions & 10 deletions flang/lib/Lower/OpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1041,14 +1041,6 @@ static mlir::Value genLogicalCombiner(fir::FirOpBuilder &builder,
return builder.create<fir::ConvertOp>(loc, value1.getType(), combined);
}

static mlir::Value loadIfRef(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::Value value) {
if (mlir::isa<fir::ReferenceType, fir::PointerType, fir::HeapType>(
value.getType()))
return builder.create<fir::LoadOp>(loc, value);
return value;
}

static mlir::Value genComparisonCombiner(fir::FirOpBuilder &builder,
mlir::Location loc,
mlir::arith::CmpIPredicate pred,
Expand All @@ -1066,8 +1058,8 @@ static mlir::Value genScalarCombiner(fir::FirOpBuilder &builder,
mlir::acc::ReductionOperator op,
mlir::Type ty, mlir::Value value1,
mlir::Value value2) {
value1 = loadIfRef(builder, loc, value1);
value2 = loadIfRef(builder, loc, value2);
value1 = builder.loadIfRef(loc, value1);
value2 = builder.loadIfRef(loc, value2);
if (op == mlir::acc::ReductionOperator::AccAdd) {
if (ty.isIntOrIndex())
return builder.create<mlir::arith::AddIOp>(loc, value1, value2);
Expand Down
7 changes: 7 additions & 0 deletions flang/lib/Optimizer/Builder/FIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "flang/Optimizer/Builder/Todo.h"
#include "flang/Optimizer/Dialect/FIRAttr.h"
#include "flang/Optimizer/Dialect/FIROpsSupport.h"
#include "flang/Optimizer/Dialect/FIRType.h"
#include "flang/Optimizer/Support/FatalError.h"
#include "flang/Optimizer/Support/InternalNames.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
Expand Down Expand Up @@ -404,6 +405,12 @@ void fir::FirOpBuilder::createStoreWithConvert(mlir::Location loc,
create<fir::StoreOp>(loc, cast, addr);
}

mlir::Value fir::FirOpBuilder::loadIfRef(mlir::Location loc, mlir::Value val) {
if (fir::isa_ref_type(val.getType()))
return create<fir::LoadOp>(loc, val);
return val;
}

fir::StringLitOp fir::FirOpBuilder::createStringLitOp(mlir::Location loc,
llvm::StringRef data) {
auto type = fir::CharacterType::get(getContext(), 1, data.size());
Expand Down

0 comments on commit 860a400

Please sign in to comment.