Skip to content

Instantly share code, notes, and snippets.

@shawnl
Last active June 9, 2020 15:48
Show Gist options
  • Select an option

  • Save shawnl/11644613a8afca6f9336662b39c22487 to your computer and use it in GitHub Desktop.

Select an option

Save shawnl/11644613a8afca6f9336662b39c22487 to your computer and use it in GitHub Desktop.
namespace mlir {
namespace toy {
/// This class adds forward constant propogation to mlir::Value
class EagerValue : public mlir::Value {
public:
/// The enumeration represents if generating this Value will cause any side-effects
/// that cannot be emulated at compile-time.
enum class Comptime {
Static = 0,
Runtime = 1,
};
/// This value represents the 'owner' of the value and its kind. See the
/// 'Kind' enumeration in mlir::Value for a more detailed description of each kind of
/// owner. Also the 'Comptime' enumeration above.
struct ImplTypeTraits : public llvm::PointerLikeTypeTraits<mlir::Value> {
// We know that all pointers within the ImplType are aligned by 8-bytes,
// meaning that we can steal up to 3 bits for the different values.
// mlir::Value already stole 2 bits for Kind, so 1 is left.
static constexpr int NumLowBitsAvailable = 1;
};
using ImplType = llvm::PointerIntPair<mlir::Value, 1, Comptime, ImplTypeTraits>;
public:
constexpr EagerValue(std::nullptr_t) : ownerAndKindAndComptime() {}
EagerValue(ImplType ownerAndKindAndComptime = {}) : ownerAndKindAndComptime(ownerAndKindAndComptime) {}
EagerValue(const EagerValue &) = default;
EagerValue &operator=(const EagerValue &) = default;
template <typename U> bool isa() const {
assert(*this && "isa<> used on a null type.");
return U::classof(*this);
}
template <typename U> U dyn_cast() const {
return isa<U>() ? U(ownerAndKind) : U(nullptr);
}
template <typename U> U dyn_cast_or_null() const {
return (*this && isa<U>()) ? U(ownerAndKind) : U(nullptr);
}
template <typename U> U cast() const {
assert(isa<U>());
return U(ownerAndKind);
}
bool operator==(const EagerValue &other) const {
return ownerAndKindAndComptime == other.ownerAndKindAndComptime;
}
//===--------------------------------------------------------------------===//
// Utilities
/// Returns the comptime status of this value.
Comptime getComptime() const { return ownerAndKindAndComptime.getInt(); }
protected:
/// This value represents the 'owner' of the value and its kind. See the
/// 'Kind' enumeration in mlir::Value for a more detailed description of each kind of
/// owner, and also the 'Comptime' enumeration above.
ImplType ownerAndKindAndComptime;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment