2929#include " mlir/IR/Matchers.h"
3030#include " mlir/IR/OpDefinition.h"
3131#include " mlir/IR/PatternMatch.h"
32+ #include " mlir/IR/TypeRange.h"
3233#include " llvm/ADT/STLExtras.h"
3334#include " llvm/ADT/SmallVector.h"
3435#include " llvm/ADT/TypeSwitch.h"
@@ -853,6 +854,15 @@ std::vector<mlir::Value> fir::ArrayLoadOp::getExtents() {
853854 return {};
854855}
855856
857+ void fir::ArrayLoadOp::getEffects (
858+ llvm::SmallVectorImpl<
859+ mlir::SideEffects::EffectInstance<mlir::MemoryEffects::Effect>>
860+ &effects) {
861+ effects.emplace_back (mlir::MemoryEffects::Read::get (), &getMemrefMutable (),
862+ mlir::SideEffects::DefaultResource::get ());
863+ addVolatileMemoryEffects ({getMemref ().getType ()}, effects);
864+ }
865+
856866llvm::LogicalResult fir::ArrayLoadOp::verify () {
857867 auto eleTy = fir::dyn_cast_ptrOrBoxEleTy (getMemref ().getType ());
858868 auto arrTy = mlir::dyn_cast<fir::SequenceType>(eleTy);
@@ -935,6 +945,15 @@ llvm::LogicalResult fir::ArrayMergeStoreOp::verify() {
935945 return mlir::success ();
936946}
937947
948+ void fir::ArrayMergeStoreOp::getEffects (
949+ llvm::SmallVectorImpl<
950+ mlir::SideEffects::EffectInstance<mlir::MemoryEffects::Effect>>
951+ &effects) {
952+ effects.emplace_back (mlir::MemoryEffects::Write::get (), &getMemrefMutable (),
953+ mlir::SideEffects::DefaultResource::get ());
954+ addVolatileMemoryEffects ({getMemref ().getType ()}, effects);
955+ }
956+
938957// ===----------------------------------------------------------------------===//
939958// ArrayFetchOp
940959// ===----------------------------------------------------------------------===//
@@ -1322,6 +1341,36 @@ mlir::ParseResult fir::CmpcOp::parse(mlir::OpAsmParser &parser,
13221341 return parseCmpOp<fir::CmpcOp>(parser, result);
13231342}
13241343
1344+ // ===----------------------------------------------------------------------===//
1345+ // VolatileCastOp
1346+ // ===----------------------------------------------------------------------===//
1347+
1348+ llvm::LogicalResult fir::VolatileCastOp::verify () {
1349+ mlir::Type fromType = getValue ().getType ();
1350+ mlir::Type toType = getType ();
1351+ // Other than volatility, are the types identical?
1352+ const bool sameBaseType =
1353+ llvm::TypeSwitch<mlir::Type, bool >(fromType)
1354+ .Case <fir::BoxType, fir::ReferenceType, fir::ClassType>(
1355+ [&](auto type) {
1356+ using TYPE = decltype (type);
1357+ return mlir::isa<TYPE>(toType);
1358+ })
1359+ .Default ([=](mlir::Type) { return fromType == toType; });
1360+ const bool sameElementType = fir::dyn_cast_ptrOrBoxEleTy (fromType) ==
1361+ fir::dyn_cast_ptrOrBoxEleTy (toType);
1362+ if (!sameBaseType || !sameElementType)
1363+ return emitOpError (" types must be identical except for volatility " )
1364+ << fromType << " / " << toType;
1365+ return mlir::success ();
1366+ }
1367+
1368+ mlir::OpFoldResult fir::VolatileCastOp::fold (FoldAdaptor adaptor) {
1369+ if (getValue ().getType () == getType ())
1370+ return getValue ();
1371+ return {};
1372+ }
1373+
13251374// ===----------------------------------------------------------------------===//
13261375// ConvertOp
13271376// ===----------------------------------------------------------------------===//
@@ -1461,7 +1510,13 @@ bool fir::ConvertOp::canBeConverted(mlir::Type inType, mlir::Type outType) {
14611510}
14621511
14631512llvm::LogicalResult fir::ConvertOp::verify () {
1464- if (canBeConverted (getValue ().getType (), getType ()))
1513+ mlir::Type inType = getValue ().getType ();
1514+ mlir::Type outType = getType ();
1515+ if (fir::isa_volatile_type (inType) != fir::isa_volatile_type (outType))
1516+ return emitOpError (" cannot convert between volatile and non-volatile "
1517+ " types, use fir.volatile_cast instead " )
1518+ << inType << " / " << outType;
1519+ if (canBeConverted (inType, outType))
14651520 return mlir::success ();
14661521 return emitOpError (" invalid type conversion" )
14671522 << getValue ().getType () << " / " << getType ();
@@ -1787,6 +1842,10 @@ llvm::LogicalResult fir::EmboxOp::verify() {
17871842 return emitOpError (" slice must not be provided for a scalar" );
17881843 if (getSourceBox () && !mlir::isa<fir::ClassType>(getResult ().getType ()))
17891844 return emitOpError (" source_box must be used with fir.class result type" );
1845+ if (fir::isa_volatile_type (getMemref ().getType ()) !=
1846+ fir::isa_volatile_type (getResult ().getType ()))
1847+ return emitOpError (" cannot convert between volatile and non-volatile "
1848+ " types, use fir.volatile_cast instead" );
17901849 return mlir::success ();
17911850}
17921851
@@ -2599,6 +2658,15 @@ void fir::LoadOp::print(mlir::OpAsmPrinter &p) {
25992658 p << " : " << getMemref ().getType ();
26002659}
26012660
2661+ void fir::LoadOp::getEffects (
2662+ llvm::SmallVectorImpl<
2663+ mlir::SideEffects::EffectInstance<mlir::MemoryEffects::Effect>>
2664+ &effects) {
2665+ effects.emplace_back (mlir::MemoryEffects::Read::get (), &getMemrefMutable (),
2666+ mlir::SideEffects::DefaultResource::get ());
2667+ addVolatileMemoryEffects ({getMemref ().getType ()}, effects);
2668+ }
2669+
26022670// ===----------------------------------------------------------------------===//
26032671// DoLoopOp
26042672// ===----------------------------------------------------------------------===//
@@ -3951,6 +4019,15 @@ void fir::StoreOp::build(mlir::OpBuilder &builder, mlir::OperationState &result,
39514019 build (builder, result, value, memref, {});
39524020}
39534021
4022+ void fir::StoreOp::getEffects (
4023+ llvm::SmallVectorImpl<
4024+ mlir::SideEffects::EffectInstance<mlir::MemoryEffects::Effect>>
4025+ &effects) {
4026+ effects.emplace_back (mlir::MemoryEffects::Write::get (), &getMemrefMutable (),
4027+ mlir::SideEffects::DefaultResource::get ());
4028+ addVolatileMemoryEffects ({getMemref ().getType ()}, effects);
4029+ }
4030+
39544031// ===----------------------------------------------------------------------===//
39554032// CopyOp
39564033// ===----------------------------------------------------------------------===//
@@ -3971,6 +4048,19 @@ llvm::LogicalResult fir::CopyOp::verify() {
39714048 return mlir::success ();
39724049}
39734050
4051+ void fir::CopyOp::getEffects (
4052+ llvm::SmallVectorImpl<
4053+ mlir::SideEffects::EffectInstance<mlir::MemoryEffects::Effect>>
4054+ &effects) {
4055+ effects.emplace_back (mlir::MemoryEffects::Read::get (), &getSourceMutable (),
4056+ mlir::SideEffects::DefaultResource::get ());
4057+ effects.emplace_back (mlir::MemoryEffects::Write::get (),
4058+ &getDestinationMutable (),
4059+ mlir::SideEffects::DefaultResource::get ());
4060+ addVolatileMemoryEffects ({getDestination ().getType (), getSource ().getType ()},
4061+ effects);
4062+ }
4063+
39744064// ===----------------------------------------------------------------------===//
39754065// StringLitOp
39764066// ===----------------------------------------------------------------------===//
0 commit comments