Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Differential revision: https://reviews.llvm.org/D136889
- Loading branch information
Showing
17 changed files
with
375 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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 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,29 @@ | ||
//===- AliasAnalysis.h - Alias Analysis in FIR -----------------*- C++ -*-===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef FIR_ANALYSIS_ALIASANALYSIS_H_ | ||
#define FIR_ANALYSIS_ALIASANALYSIS_H_ | ||
|
||
#include "mlir/Analysis/AliasAnalysis.h" | ||
|
||
namespace fir { | ||
|
||
//===----------------------------------------------------------------------===// | ||
// AliasAnalysis | ||
//===----------------------------------------------------------------------===// | ||
class AliasAnalysis { | ||
public: | ||
/// Given two values, return their aliasing behavior. | ||
mlir::AliasResult alias(mlir::Value lhs, mlir::Value rhs); | ||
|
||
/// Return the modify-reference behavior of `op` on `location`. | ||
mlir::ModRefResult getModRef(mlir::Operation *op, mlir::Value location); | ||
}; | ||
} // namespace fir | ||
|
||
#endif // FIR_ANALYSIS_ALIASANALYSIS_H_ |
This file contains 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 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,67 @@ | ||
//===- AliasAnalysis.cpp - Alias Analysis for FIR ------------------------===// | ||
// | ||
// 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 "flang/Optimizer/Analysis/AliasAnalysis.h" | ||
#include "mlir/Interfaces/SideEffectInterfaces.h" | ||
|
||
using namespace mlir; | ||
|
||
//===----------------------------------------------------------------------===// | ||
// AliasAnalysis: alias | ||
//===----------------------------------------------------------------------===// | ||
|
||
namespace fir { | ||
AliasResult AliasAnalysis::alias(Value lhs, Value rhs) { | ||
// This is for now a mock analysis | ||
if (lhs == rhs) { | ||
return AliasResult::MustAlias; | ||
} | ||
return AliasResult::MayAlias; | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// AliasAnalysis: getModRef | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// This is mostly inspired by MLIR::LocalAliasAnalysis with 2 notable | ||
/// differences 1) Regions are not handled here but will be handled by a data | ||
/// flow analysis to come 2) Allocate and Free effects are considered modifying | ||
ModRefResult AliasAnalysis::getModRef(Operation *op, Value location) { | ||
MemoryEffectOpInterface interface = dyn_cast<MemoryEffectOpInterface>(op); | ||
if (!interface) | ||
return ModRefResult::getModAndRef(); | ||
|
||
// Build a ModRefResult by merging the behavior of the effects of this | ||
// operation. | ||
SmallVector<MemoryEffects::EffectInstance> effects; | ||
interface.getEffects(effects); | ||
|
||
ModRefResult result = ModRefResult::getNoModRef(); | ||
for (const MemoryEffects::EffectInstance &effect : effects) { | ||
|
||
// Check for an alias between the effect and our memory location. | ||
AliasResult aliasResult = AliasResult::MayAlias; | ||
if (Value effectValue = effect.getValue()) | ||
aliasResult = alias(effectValue, location); | ||
|
||
// If we don't alias, ignore this effect. | ||
if (aliasResult.isNo()) | ||
continue; | ||
|
||
// Merge in the corresponding mod or ref for this effect. | ||
if (isa<MemoryEffects::Read>(effect.getEffect())) { | ||
result = result.merge(ModRefResult::getRef()); | ||
} else { | ||
result = result.merge(ModRefResult::getMod()); | ||
} | ||
if (result.isModAndRef()) | ||
break; | ||
} | ||
return result; | ||
} | ||
} // namespace fir |
This file contains 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,16 @@ | ||
add_flang_library(FIRAnalysis | ||
AliasAnalysis.cpp | ||
|
||
DEPENDS | ||
FIRBuilder | ||
FIRDialect | ||
FIRSupport | ||
|
||
LINK_LIBS | ||
FIRBuilder | ||
FIRDialect | ||
MLIRFuncDialect | ||
MLIRLLVMDialect | ||
MLIRMathTransforms | ||
FIRSupport | ||
) |
This file contains 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 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 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,29 @@ | ||
# Exclude tests from libMLIR.so | ||
add_flang_library(FIRTestAnalysis | ||
TestAliasAnalysis.cpp | ||
|
||
DEPENDS | ||
FIRDialect | ||
FIRBuilder | ||
FIRSupport | ||
FIRTransforms | ||
FIRAnalysis | ||
${dialect_libs} | ||
|
||
LINK_LIBS | ||
FIRDialect | ||
FIRBuilder | ||
FIRSupport | ||
FIRTransforms | ||
FIRAnalysis | ||
${dialect_libs} | ||
MLIRFuncDialect | ||
MLIRLLVMDialect | ||
MLIRAnalysis | ||
MLIRTestAnalysis | ||
) | ||
|
||
target_include_directories(FIRTestAnalysis | ||
PRIVATE | ||
${MLIR_MAIN_SRC_DIR}/.. | ||
) |
72 changes: 72 additions & 0 deletions
72
flang/test/lib/Analysis/AliasAnalysis/TestAliasAnalysis.cpp
This file contains 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,72 @@ | ||
//===- TestAliasAnalysis.cpp - Test FIR lias analysis -----------------===// | ||
// | ||
// 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 "mlir/test/lib/Analysis/TestAliasAnalysis.h" | ||
#include "mlir/Analysis/AliasAnalysis.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "flang/Optimizer/Analysis/AliasAnalysis.h" | ||
|
||
using namespace mlir; | ||
|
||
namespace { | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Testing AliasResult | ||
//===----------------------------------------------------------------------===// | ||
|
||
struct TestFIRAliasAnalysisPass | ||
: public test::TestAliasAnalysisBase, | ||
PassWrapper<TestFIRAliasAnalysisPass, OperationPass<>> { | ||
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestFIRAliasAnalysisPass) | ||
|
||
StringRef getArgument() const final { return "test-fir-alias-analysis"; } | ||
StringRef getDescription() const final { | ||
return "Test alias analysis results."; | ||
} | ||
void runOnOperation() override { | ||
mlir::AliasAnalysis aliasAnalysis(getOperation()); | ||
aliasAnalysis.addAnalysisImplementation(fir::AliasAnalysis()); | ||
runAliasAnalysisOnOperation(getOperation(), aliasAnalysis); | ||
} | ||
}; | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Testing ModRefResult | ||
//===----------------------------------------------------------------------===// | ||
|
||
struct TestFIRAliasAnalysisModRefPass | ||
: public test::TestAliasAnalysisModRefBase, | ||
PassWrapper<TestFIRAliasAnalysisModRefPass, OperationPass<>> { | ||
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestFIRAliasAnalysisModRefPass) | ||
|
||
StringRef getArgument() const final { | ||
return "test-fir-alias-analysis-modref"; | ||
} | ||
StringRef getDescription() const final { | ||
return "Test alias analysis ModRef results."; | ||
} | ||
void runOnOperation() override { | ||
mlir::AliasAnalysis aliasAnalysis(getOperation()); | ||
aliasAnalysis.addAnalysisImplementation(fir::AliasAnalysis()); | ||
runAliasAnalysisOnOperation(getOperation(), aliasAnalysis); | ||
} | ||
}; | ||
} // namespace | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Pass Registration | ||
//===----------------------------------------------------------------------===// | ||
|
||
namespace fir { | ||
namespace test { | ||
void registerTestFIRAliasAnalysisPass() { | ||
PassRegistration<TestFIRAliasAnalysisPass>(); | ||
PassRegistration<TestFIRAliasAnalysisModRefPass>(); | ||
} | ||
} // namespace test | ||
} // namespace fir |
21 changes: 21 additions & 0 deletions
21
flang/test/lib/Analysis/AliasAnalysis/alias-analysis-1.fir
This file contains 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,21 @@ | ||
// RUN: fir-opt %s -pass-pipeline='builtin.module(func.func(test-fir-alias-analysis))' -split-input-file 2>&1 | FileCheck %s | ||
|
||
// CHECK-LABEL: Testing : "_QPtest" | ||
// CHECK-DAG: alloca_1#0 <-> address_of#0: MayAlias | ||
func.func @_QPtest(%arg1: !fir.ref<i32>) { | ||
%c1_i32 = arith.constant 1 : i32 | ||
%0 = fir.alloca () -> () {test.ptr = "alloca_1"} | ||
%1 = fir.address_of(@_QPf) {test.ptr = "address_of"} : () -> i32 | ||
%2 = fir.convert %1 : (() -> i32) -> (() -> ()) | ||
%4 = fir.convert %0 : (!fir.ref<() -> ()>) -> !fir.llvm_ptr<() -> ()> | ||
fir.store %2 to %4 : !fir.llvm_ptr<() -> ()> | ||
%6 = fir.load %0 : !fir.ref<() -> ()> | ||
fir.call @_QPs(%6) : (() -> ()) -> () | ||
return | ||
} | ||
|
||
// ----- | ||
func.func private @_QPs(%arg0: () -> ()) | ||
|
||
// ----- | ||
func.func private @_QPf() -> i32 |
This file contains 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 @@ | ||
add_subdirectory(AliasAnalysis) |
This file contains 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 @@ | ||
add_subdirectory(Analysis) |
This file contains 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,7 @@ | ||
|
||
# Excluding .cpp file from the extensions since from this level down they are used for the development | ||
config.suffixes = ['.c', '.f', '.F', '.ff', '.FOR', '.for', '.f77', '.f90', '.F90', | ||
'.ff90', '.f95', '.F95', '.ff95', '.fpp', '.FPP', '.cuf' | ||
'.CUF', '.f18', '.F18', '.f03', '.F03', '.f08', '.F08', | ||
'.ll', '.fir', '.mlir'] | ||
|
This file contains 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 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
Oops, something went wrong.