Enzyme main
Loading...
Searching...
No Matches
ActivityAnalysis.cpp File Reference
#include <cstdint>
#include <deque>
#include <llvm/Config/llvm-config.h>
#include <memory>
#include "llvm/ADT/ImmutableSet.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/InlineAsm.h"
#include "ActivityAnalysis.h"
#include "Utils.h"
#include "llvm/Demangle/Demangle.h"
#include "FunctionUtils.h"
#include "LibraryFuncs.h"
#include "TypeAnalysis/TBAA.h"
#include "llvm/Analysis/ValueTracking.h"
#include <map>
#include <set>
#include <unordered_map>
#include "ActivityAnalysis.h"
#include "Interfaces/GradientUtils.h"
#include "Interfaces/Utils.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/Matchers.h"
#include "mlir/IR/Region.h"
#include "mlir/IR/RegionKindInterface.h"
#include "mlir/IR/SymbolTable.h"
#include "mlir/Interfaces/CallInterfaces.h"
#include "mlir/Interfaces/CastInterfaces.h"
#include "mlir/Interfaces/ControlFlowInterfaces.h"
#include "mlir/Interfaces/SideEffectInterfaces.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/Support/ModRef.h"
#include "Interfaces/AutoDiffOpInterface.h"
Include dependency graph for ActivityAnalysis.cpp:

Go to the source code of this file.

Macros

#define addAttribute   addAttributeAtIndex
 
#define removeAttribute   removeAttributeAtIndex
 
#define getAttribute   getAttributeAtIndex
 
#define hasAttribute   hasAttributeAtIndex
 

Functions

cl::opt< bool > EnzymePrintActivity ("enzyme-print-activity", cl::init(false), cl::Hidden, cl::desc("Print activity analysis algorithm"))
 
cl::opt< bool > EnzymeNonmarkedGlobalsInactive ("enzyme-globals-default-inactive", cl::init(false), cl::Hidden, cl::desc("Consider all nonmarked globals to be inactive"))
 
cl::opt< bool > EnzymeEmptyFnInactive ("enzyme-emptyfn-inactive", cl::init(false), cl::Hidden, cl::desc("Empty functions are considered inactive"))
 
cl::opt< bool > EnzymeGlobalActivity ("enzyme-global-activity", cl::init(false), cl::Hidden, cl::desc("Enable correct global activity analysis"))
 
cl::opt< bool > EnzymeDisableActivityAnalysis ("enzyme-disable-activity-analysis", cl::init(false), cl::Hidden, cl::desc("Disable activity analysis"))
 
cl::opt< bool > EnzymeEnableRecursiveHypotheses ("enzyme-enable-recursive-activity", cl::init(true), cl::Hidden, cl::desc("Enable re-evaluation of activity analysis from updated results"))
 
bool isInactiveCall (CallBase &CI)
 Return whether the call is always inactive by definition.
 
bool isInactiveCallInst (CallBase &CB, llvm::TargetLibraryInfo &TLI)
 
static void propagateArgumentInformation (TargetLibraryInfo &TLI, CallInst &CI, llvm::function_ref< bool(Value *)> propagateFromOperand)
 Call the function propagateFromOperand on all operands of CI that could impact the activity of the call instruction.
 
bool isValuePotentiallyUsedAsPointer (llvm::Value *val)
 
static Operation * getFunctionFromCall (CallOpInterface iface)
 
static bool isReadOnly (Operation *op)
 
static bool isFunctionReturn (Operation *op)
 
static bool isValuePotentiallyUsedAsPointer (Value val)
 
static Value getUnderlyingObject (mlir::Value value, unsigned maxLookup)
 
static bool mayAllocateMemory (Operation *op)
 
static bool mayReadFromMemory (Operation *op)
 
static bool mayWriteToMemory (Operation *op)
 
static FunctionOpInterface getFunctionIfArgument (Value value)
 
static std::optional< SmallVector< Value > > getPotentialTerminatorUsers (Operation *op, Value parent)
 
static SmallVector< Value > getPotentialIncomingValues (OpResult res)
 
static SmallVector< Value > getPotentialIncomingValues (BlockArgument arg)
 
static Operation * getAncestorInRegion (Operation *op, Region *region)
 Return an ancestor of op that resides in the given region or nullptr if there is no such ancestor.
 
static void allFollowersOf (Operation *op, function_ref< WalkResult(Operation *)> f)
 Call f on all operations that may be executed after op.
 

Variables

static const StringSet InactiveGlobals
 
const llvm::StringMap< size_t > MPIInactiveCommAllocators
 
static const char * KnownInactiveFunctionsStartingWith []
 
static const char * KnownInactiveFunctionsContains []
 
static const std::set< std::string > InactiveGlobals
 
static const std::set< std::string > KnownInactiveFunctionInsts
 
static const std::set< std::string > KnownInactiveFunctions
 
static const char * DemangledKnownInactiveFunctionsStartingWith []
 
static const unsigned constantIntrinsics []
 
constexpr bool EnzymePrintActivity = false
 

Macro Definition Documentation

◆ addAttribute

#define addAttribute   addAttributeAtIndex

◆ getAttribute

#define getAttribute   getAttributeAtIndex

◆ hasAttribute

#define hasAttribute   hasAttributeAtIndex

◆ removeAttribute

#define removeAttribute   removeAttributeAtIndex

Function Documentation

◆ allFollowersOf()

static void allFollowersOf ( Operation * op,
function_ref< WalkResult(Operation *)> f )
static

Call f on all operations that may be executed after op.

Definition at line 1219 of file ActivityAnalysis.cpp.

References getAncestorInRegion().

Referenced by mlir::enzyme::ActivityAnalyzer::isConstantValue().

◆ EnzymeDisableActivityAnalysis()

cl::opt< bool > EnzymeDisableActivityAnalysis ( "enzyme-disable-activity-analysis" ,
cl::init(false) ,
cl::Hidden ,
cl::desc("Disable activity analysis")  )

◆ EnzymeEmptyFnInactive()

cl::opt< bool > EnzymeEmptyFnInactive ( "enzyme-emptyfn-inactive" ,
cl::init(false) ,
cl::Hidden ,
cl::desc("Empty functions are considered inactive")  )

◆ EnzymeEnableRecursiveHypotheses()

cl::opt< bool > EnzymeEnableRecursiveHypotheses ( "enzyme-enable-recursive-activity" ,
cl::init(true) ,
cl::Hidden ,
cl::desc("Enable re-evaluation of activity analysis from updated results")  )

◆ EnzymeGlobalActivity()

cl::opt< bool > EnzymeGlobalActivity ( "enzyme-global-activity" ,
cl::init(false) ,
cl::Hidden ,
cl::desc("Enable correct global activity analysis")  )

◆ EnzymeNonmarkedGlobalsInactive()

cl::opt< bool > EnzymeNonmarkedGlobalsInactive ( "enzyme-globals-default-inactive" ,
cl::init(false) ,
cl::Hidden ,
cl::desc("Consider all nonmarked globals to be inactive")  )

◆ EnzymePrintActivity()

cl::opt< bool > EnzymePrintActivity ( "enzyme-print-activity" ,
cl::init(false) ,
cl::Hidden ,
cl::desc("Print activity analysis algorithm")  )

◆ getAncestorInRegion()

static Operation * getAncestorInRegion ( Operation * op,
Region * region )
static

Return an ancestor of op that resides in the given region or nullptr if there is no such ancestor.

Definition at line 1211 of file ActivityAnalysis.cpp.

Referenced by allFollowersOf().

◆ getFunctionFromCall()

◆ getFunctionIfArgument()

static FunctionOpInterface getFunctionIfArgument ( Value value)
static

◆ getPotentialIncomingValues() [1/2]

static SmallVector< Value > getPotentialIncomingValues ( BlockArgument arg)
static

Definition at line 1095 of file ActivityAnalysis.cpp.

◆ getPotentialIncomingValues() [2/2]

static SmallVector< Value > getPotentialIncomingValues ( OpResult res)
static

◆ getPotentialTerminatorUsers()

static std::optional< SmallVector< Value > > getPotentialTerminatorUsers ( Operation * op,
Value parent )
static

◆ getUnderlyingObject()

static Value getUnderlyingObject ( mlir::Value value,
unsigned maxLookup )
static

◆ isFunctionReturn()

static bool isFunctionReturn ( Operation * op)
static

◆ isInactiveCall()

bool isInactiveCall ( CallBase & CI)

Return whether the call is always inactive by definition.

Definition at line 166 of file ActivityAnalysis.cpp.

◆ isInactiveCallInst()

bool isInactiveCallInst ( CallBase & CB,
llvm::TargetLibraryInfo & TLI )

Definition at line 532 of file ActivityAnalysis.cpp.

◆ isReadOnly()

◆ isValuePotentiallyUsedAsPointer() [1/2]

bool isValuePotentiallyUsedAsPointer ( llvm::Value * val)

◆ isValuePotentiallyUsedAsPointer() [2/2]

static bool isValuePotentiallyUsedAsPointer ( Value val)
static

Definition at line 845 of file ActivityAnalysis.cpp.

References EnzymePrintActivity, and isFunctionReturn().

◆ mayAllocateMemory()

static bool mayAllocateMemory ( Operation * op)
static

◆ mayReadFromMemory()

static bool mayReadFromMemory ( Operation * op)
static

◆ mayWriteToMemory()

static bool mayWriteToMemory ( Operation * op)
static

◆ propagateArgumentInformation()

static void propagateArgumentInformation ( TargetLibraryInfo & TLI,
CallInst & CI,
llvm::function_ref< bool(Value *)> propagateFromOperand )
inlinestatic

Call the function propagateFromOperand on all operands of CI that could impact the activity of the call instruction.

Only the first argument (magnitude) of copysign is active

Definition at line 661 of file ActivityAnalysis.cpp.

References getFunctionFromCall().

Variable Documentation

◆ constantIntrinsics

const unsigned constantIntrinsics[]
static

Definition at line 214 of file ActivityAnalysis.cpp.

◆ DemangledKnownInactiveFunctionsStartingWith

const char* DemangledKnownInactiveFunctionsStartingWith[]
static

◆ EnzymePrintActivity

◆ InactiveGlobals [1/2]

const StringSet InactiveGlobals
static

Definition at line 107 of file ActivityAnalysis.cpp.

◆ InactiveGlobals [2/2]

const std::set<std::string> InactiveGlobals
static
Initial value:
= {
"ompi_request_null", "ompi_mpi_double", "ompi_mpi_comm_world", "stderr",
"stdout", "stdin", "_ZSt3cin", "_ZSt4cout", "_ZSt5wcout", "_ZSt4cerr",
"_ZTVNSt7__cxx1115basic_stringbufIcSt11char_traitsIcESaIcEEE",
"_ZTVSt15basic_streambufIcSt11char_traitsIcEE",
"_ZTVSt9basic_iosIcSt11char_traitsIcEE",
"_ZTVNSt7__cxx1119basic_istringstreamIcSt11char_traitsIcESaIcEEE",
"_ZTTNSt7__cxx1119basic_istringstreamIcSt11char_traitsIcESaIcEEE",
"_ZTVNSt7__cxx1119basic_ostringstreamIcSt11char_traitsIcESaIcEEE",
"_ZTTNSt7__cxx1119basic_ostringstreamIcSt11char_traitsIcESaIcEEE",
"_ZTVNSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEEE",
"_ZTTNSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEEE",
"_ZTTSt14basic_ifstreamIcSt11char_traitsIcEE",
"_ZTTSt14basic_ofstreamIcSt11char_traitsIcEE",
"_ZTVN10__cxxabiv120__si_class_type_infoE",
"_ZTVN10__cxxabiv117__class_type_infoE",
"_ZTVN10__cxxabiv121__vmi_class_type_infoE"}

Definition at line 37 of file ActivityAnalysis.cpp.

◆ KnownInactiveFunctionInsts

const std::set<std::string> KnownInactiveFunctionInsts
static
Initial value:
= {
"__dynamic_cast",
"_ZSt18_Rb_tree_decrementPKSt18_Rb_tree_node_base",
"_ZSt18_Rb_tree_incrementPKSt18_Rb_tree_node_base",
"_ZSt18_Rb_tree_decrementPSt18_Rb_tree_node_base",
"_ZSt18_Rb_tree_incrementPSt18_Rb_tree_node_base",
"jl_ptr_to_array",
"jl_ptr_to_array_1d"}

Definition at line 79 of file ActivityAnalysis.cpp.

Referenced by mlir::enzyme::ActivityAnalyzer::isConstantValue().

◆ KnownInactiveFunctions

const std::set<std::string> KnownInactiveFunctions
static

◆ KnownInactiveFunctionsContains

const char* KnownInactiveFunctionsContains[]
static
Initial value:
= {
"__enzyme_float", "__enzyme_double", "__enzyme_integer",
"__enzyme_pointer"}

Definition at line 33 of file ActivityAnalysis.cpp.

Referenced by mlir::enzyme::ActivityAnalyzer::isConstantValue().

◆ KnownInactiveFunctionsStartingWith

const char* KnownInactiveFunctionsStartingWith[]
static
Initial value:
= {
"f90io",
"$ss5print",
"_ZTv0_n24_NSoD",
"_ZNSt16allocator_traitsISaIdEE10deallocate",
"_ZNSaIcED1Ev",
"_ZNSaIcEC1Ev",
}

Definition at line 24 of file ActivityAnalysis.cpp.

Referenced by mlir::enzyme::ActivityAnalyzer::isConstantValue().

◆ MPIInactiveCommAllocators

static const std::map< std::string, size_t > MPIInactiveCommAllocators
Initial value:
= {
{"MPI_Graph_create", 5},
{"MPI_Comm_split", 2},
{"MPI_Intercomm_create", 6},
{"MPI_Comm_spawn", 6},
{"MPI_Comm_spawn_multiple", 7},
{"MPI_Comm_accept", 4},
{"MPI_Comm_connect", 4},
{"MPI_Comm_create", 2},
{"MPI_Comm_create_group", 3},
{"MPI_Comm_dup", 1},
{"MPI_Comm_dup", 2},
{"MPI_Comm_idup", 1},
{"MPI_Comm_join", 1},
}

Definition at line 148 of file ActivityAnalysis.cpp.

Referenced by AdjointGenerator::handleKnownCallDerivatives(), AdjointGenerator::handleMPI(), and mlir::enzyme::ActivityAnalyzer::isConstantValue().