LLVM: llvm::mdconst Namespace Reference (original) (raw)

Transitional API for extracting constants from Metadata.

This namespace contains transitional functions for metadata that points to Constants.

In prehistory – when metadata was a subclass of ValueMDNode operands could refer to any Value. There's was a lot of code like this:

decltype(auto) dyn_cast(const From &Val)

dyn_cast - Return the argument parameter cast to the specified type.

Now that Value and Metadata are in separate hierarchies, maintaining the semantics for isa(), cast(), dyn_cast() (etc.) requires three steps: cast in the Metadata hierarchy, extraction of the Value, and cast in the Value hierarchy. Besides creating boiler-plate, this requires subtle control flow changes.

The end-goal is to create a new type of metadata, called (e.g.) MDInt, so that metadata can refer to numbers without traversing a bridge to the Value hierarchy. In this final state, the code above would look like this:

The API in this namespace supports the transition. MDInt doesn't exist yet, and even once it does, changing each metadata schema to use it is its own mini-project. In the meantime this API prevents us from introducing complex and bug-prone control flow that will disappear in the end. In particular, the above code looks like this:

auto *CI = mdconst::dyn_extract(N->getOperand(2));

std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract(Y &&MD)

Extract a Value from Metadata, if any.

The full set of provided functions includes:

mdconst::hasa <=> isa mdconst::extract <=> cast mdconst::extract_or_null <=> cast_or_null mdconst::dyn_extract <=> dyn_cast mdconst::dyn_extract_or_null <=> dyn_cast_or_null

The target of the cast must be a subclass of Constant.