Enzyme main
Loading...
Searching...
No Matches
Utils.h
Go to the documentation of this file.
1//===- Utils.h - Utilities for gradient interfaces -------* C++
2//-*-===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9#pragma once
10
11#include "mlir/IR/Builders.h"
12#include "mlir/IR/Operation.h"
13#include "mlir/Interfaces/FunctionInterfaces.h"
14#include "mlir/Interfaces/SideEffectInterfaces.h"
15
16namespace mlir {
17namespace enzyme {
18class MGradientUtils;
19namespace oputils {
20
21const std::set<std::string> &getNonCapturingFunctions();
22
23// Checks whether a value escapes through its uses, following view results.
24// If seenuse is provided, sets it to true when potentialUser is encountered.
25bool isCaptured(Value v, Operation *potentialUser = nullptr,
26 bool *seenuse = nullptr);
27
28// Checks if the operation/function has any memory write effects. This enables
29// batching specific AD optimiziations(which are triggered only if the primal
30// function doesnt modify memory operands)
31bool isReadOnly(Operation *op);
32
33bool isReadNone(Operation *op);
34
35// The shadow of a stored value nothing differentiates is the primal value
36// itself: the shadow memory's structural fields must read as the primal's.
37// If the activity given was wrong about the value, only runtime activity
38// could catch it; every site says so through here, so a future runtime
39// activity implementation has a single seam to take over (as EnzymeLLVM's
40// does).
41Value inactiveStoredValueShadow(Operation *orig, MGradientUtils &gutils,
42 Value stored, OpBuilder &builder);
43
44// Walks a pointer-like value to the object it is derived from, looking
45// through view-like ops (memref.cast, memref.memory_space_cast, subviews),
46// llvm.getelementptr, llvm.bitcast, and llvm.addrspacecast. If offsetAllowed is
47// false, only follow operations known to preserve the starting address.
48Value getBaseObject(Value v, bool offsetAllowed = true);
49
50// Checks if 2 values v1 and v2 may alias with each other locally
51bool mayAlias(Value v1, Value v2);
52
53// check if 2 memory effects' underlying values alias with each other
54bool mayAlias(MemoryEffects::EffectInstance &A,
55 MemoryEffects::EffectInstance &B);
56
57// check if a memory effect's underlying values alias with a value
58bool mayAlias(mlir::MemoryEffects::EffectInstance a, mlir::Value v2);
59
60/// Returns the side effects of an operation(similar to
61/// `mlir::getEffectsRecursively`). If the operation has RecursiveMemoryEffects,
62/// include all side effects of child operations.
63///
64/// Also accounts for LLVM and autodiff-specific memory effects which are not
65/// captured by the default `mlir::getEffectsRecursively`
66bool collectOpEffects(Operation *rootOp,
67 SmallVector<MemoryEffects::EffectInstance> &effects);
68
69// Specialize memory effect collection for a FunctionOpInterface
70SmallVector<MemoryEffects::EffectInstance>
71collectFnEffects(FunctionOpInterface fnOp);
72
73MemoryEffects::EffectInstance getEffectOfVal(Value val,
74 MemoryEffects::Effect *effect,
75 SideEffects::Resource *resource);
76} // namespace oputils
77} // namespace enzyme
78} // namespace mlir
bool mayAlias(Value v1, Value v2)
Definition Utils.cpp:145
bool isCaptured(Value v, Operation *potentialUser, bool *seenuse)
Definition Utils.cpp:42
const std::set< std::string > & getNonCapturingFunctions()
Definition Utils.cpp:31
Value getBaseObject(Value v, bool offsetAllowed)
Definition Utils.cpp:123
bool collectOpEffects(Operation *rootOp, SmallVector< MemoryEffects::EffectInstance > &effects)
Returns the side effects of an operation(similar to mlir::getEffectsRecursively).
Definition Utils.cpp:306
SmallVector< MemoryEffects::EffectInstance > collectFnEffects(FunctionOpInterface fnOp)
Definition Utils.cpp:418
MemoryEffects::EffectInstance getEffectOfVal(Value val, MemoryEffects::Effect *effect, SideEffects::Resource *resource)
Definition Utils.cpp:431
bool isReadNone(Operation *op)
Definition Utils.cpp:275
Value inactiveStoredValueShadow(Operation *orig, MGradientUtils &gutils, Value stored, OpBuilder &builder)
Definition Utils.cpp:104
bool isReadOnly(Operation *op)
Definition Utils.cpp:246