2012-05-05 04:18:50 +08:00
|
|
|
//===- NVPTXUtilities.cpp - Utility Functions -----------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2012-05-05 04:18:50 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains miscellaneous utility functions
|
2019-07-12 16:25:59 +08:00
|
|
|
//
|
2012-05-05 04:18:50 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "NVPTXUtilities.h"
|
|
|
|
#include "NVPTX.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Operator.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2019-08-07 18:57:25 +08:00
|
|
|
#include "llvm/Support/Mutex.h"
|
2012-05-05 04:18:50 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring>
|
|
|
|
#include <map>
|
2019-08-07 18:57:25 +08:00
|
|
|
#include <mutex>
|
2012-05-05 04:18:50 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
namespace llvm {
|
2012-05-05 04:18:50 +08:00
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
namespace {
|
2012-05-05 04:18:50 +08:00
|
|
|
typedef std::map<std::string, std::vector<unsigned> > key_val_pair_t;
|
|
|
|
typedef std::map<const GlobalValue *, key_val_pair_t> global_val_annot_t;
|
|
|
|
typedef std::map<const Module *, global_val_annot_t> per_module_annot_t;
|
2016-12-15 06:32:44 +08:00
|
|
|
} // anonymous namespace
|
2012-05-05 04:18:50 +08:00
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
static ManagedStatic<per_module_annot_t> annotationCache;
|
2014-04-09 23:38:52 +08:00
|
|
|
static sys::Mutex Lock;
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
void clearAnnotationCache(const Module *Mod) {
|
2019-08-07 18:57:25 +08:00
|
|
|
std::lock_guard<sys::Mutex> Guard(Lock);
|
2014-04-09 23:38:52 +08:00
|
|
|
annotationCache->erase(Mod);
|
|
|
|
}
|
2012-05-05 04:18:50 +08:00
|
|
|
|
|
|
|
static void cacheAnnotationFromMD(const MDNode *md, key_val_pair_t &retval) {
|
2019-08-07 18:57:25 +08:00
|
|
|
std::lock_guard<sys::Mutex> Guard(Lock);
|
2012-05-05 04:18:50 +08:00
|
|
|
assert(md && "Invalid mdnode for annotation");
|
|
|
|
assert((md->getNumOperands() % 2) == 1 && "Invalid number of operands");
|
|
|
|
// start index = 1, to skip the global variable key
|
|
|
|
// increment = 2, to skip the value for each property-value pairs
|
|
|
|
for (unsigned i = 1, e = md->getNumOperands(); i != e; i += 2) {
|
|
|
|
// property
|
|
|
|
const MDString *prop = dyn_cast<MDString>(md->getOperand(i));
|
|
|
|
assert(prop && "Annotation property not a string");
|
|
|
|
|
|
|
|
// value
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
ConstantInt *Val = mdconst::dyn_extract<ConstantInt>(md->getOperand(i + 1));
|
2012-05-05 04:18:50 +08:00
|
|
|
assert(Val && "Value operand not a constant int");
|
|
|
|
|
|
|
|
std::string keyname = prop->getString().str();
|
|
|
|
if (retval.find(keyname) != retval.end())
|
|
|
|
retval[keyname].push_back(Val->getZExtValue());
|
|
|
|
else {
|
|
|
|
std::vector<unsigned> tmp;
|
|
|
|
tmp.push_back(Val->getZExtValue());
|
|
|
|
retval[keyname] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cacheAnnotationFromMD(const Module *m, const GlobalValue *gv) {
|
2019-08-07 18:57:25 +08:00
|
|
|
std::lock_guard<sys::Mutex> Guard(Lock);
|
2016-12-15 06:32:44 +08:00
|
|
|
NamedMDNode *NMD = m->getNamedMetadata("nvvm.annotations");
|
2012-05-05 04:18:50 +08:00
|
|
|
if (!NMD)
|
|
|
|
return;
|
|
|
|
key_val_pair_t tmp;
|
|
|
|
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
|
2014-11-12 05:30:22 +08:00
|
|
|
const MDNode *elem = NMD->getOperand(i);
|
2012-05-05 04:18:50 +08:00
|
|
|
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
GlobalValue *entity =
|
|
|
|
mdconst::dyn_extract_or_null<GlobalValue>(elem->getOperand(0));
|
2012-05-05 04:18:50 +08:00
|
|
|
// entity may be null due to DCE
|
|
|
|
if (!entity)
|
|
|
|
continue;
|
|
|
|
if (entity != gv)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// accumulate annotations for entity in tmp
|
|
|
|
cacheAnnotationFromMD(elem, tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp.empty()) // no annotations for this gv
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((*annotationCache).find(m) != (*annotationCache).end())
|
2014-10-05 00:55:56 +08:00
|
|
|
(*annotationCache)[m][gv] = std::move(tmp);
|
2012-05-05 04:18:50 +08:00
|
|
|
else {
|
|
|
|
global_val_annot_t tmp1;
|
2014-10-05 00:55:56 +08:00
|
|
|
tmp1[gv] = std::move(tmp);
|
|
|
|
(*annotationCache)[m] = std::move(tmp1);
|
2012-05-05 04:18:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool findOneNVVMAnnotation(const GlobalValue *gv, const std::string &prop,
|
|
|
|
unsigned &retval) {
|
2019-08-07 18:57:25 +08:00
|
|
|
std::lock_guard<sys::Mutex> Guard(Lock);
|
2012-05-05 04:18:50 +08:00
|
|
|
const Module *m = gv->getParent();
|
|
|
|
if ((*annotationCache).find(m) == (*annotationCache).end())
|
|
|
|
cacheAnnotationFromMD(m, gv);
|
|
|
|
else if ((*annotationCache)[m].find(gv) == (*annotationCache)[m].end())
|
|
|
|
cacheAnnotationFromMD(m, gv);
|
|
|
|
if ((*annotationCache)[m][gv].find(prop) == (*annotationCache)[m][gv].end())
|
|
|
|
return false;
|
|
|
|
retval = (*annotationCache)[m][gv][prop][0];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool findAllNVVMAnnotation(const GlobalValue *gv, const std::string &prop,
|
|
|
|
std::vector<unsigned> &retval) {
|
2019-08-07 18:57:25 +08:00
|
|
|
std::lock_guard<sys::Mutex> Guard(Lock);
|
2012-05-05 04:18:50 +08:00
|
|
|
const Module *m = gv->getParent();
|
|
|
|
if ((*annotationCache).find(m) == (*annotationCache).end())
|
|
|
|
cacheAnnotationFromMD(m, gv);
|
|
|
|
else if ((*annotationCache)[m].find(gv) == (*annotationCache)[m].end())
|
|
|
|
cacheAnnotationFromMD(m, gv);
|
|
|
|
if ((*annotationCache)[m][gv].find(prop) == (*annotationCache)[m][gv].end())
|
|
|
|
return false;
|
|
|
|
retval = (*annotationCache)[m][gv][prop];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool isTexture(const Value &val) {
|
2012-05-05 04:18:50 +08:00
|
|
|
if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
|
|
|
|
unsigned annot;
|
2016-12-15 06:32:44 +08:00
|
|
|
if (findOneNVVMAnnotation(gv, "texture", annot)) {
|
2012-05-05 04:18:50 +08:00
|
|
|
assert((annot == 1) && "Unexpected annotation on a texture symbol");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool isSurface(const Value &val) {
|
2012-05-05 04:18:50 +08:00
|
|
|
if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
|
|
|
|
unsigned annot;
|
2016-12-15 06:32:44 +08:00
|
|
|
if (findOneNVVMAnnotation(gv, "surface", annot)) {
|
2012-05-05 04:18:50 +08:00
|
|
|
assert((annot == 1) && "Unexpected annotation on a surface symbol");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool isSampler(const Value &val) {
|
|
|
|
const char *AnnotationName = "sampler";
|
|
|
|
|
2012-05-05 04:18:50 +08:00
|
|
|
if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
|
|
|
|
unsigned annot;
|
2016-12-15 06:32:44 +08:00
|
|
|
if (findOneNVVMAnnotation(gv, AnnotationName, annot)) {
|
2012-05-05 04:18:50 +08:00
|
|
|
assert((annot == 1) && "Unexpected annotation on a sampler symbol");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (const Argument *arg = dyn_cast<Argument>(&val)) {
|
|
|
|
const Function *func = arg->getParent();
|
|
|
|
std::vector<unsigned> annot;
|
2016-12-15 06:32:44 +08:00
|
|
|
if (findAllNVVMAnnotation(func, AnnotationName, annot)) {
|
2016-08-12 06:21:41 +08:00
|
|
|
if (is_contained(annot, arg->getArgNo()))
|
2012-05-05 04:18:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool isImageReadOnly(const Value &val) {
|
2012-05-05 04:18:50 +08:00
|
|
|
if (const Argument *arg = dyn_cast<Argument>(&val)) {
|
|
|
|
const Function *func = arg->getParent();
|
|
|
|
std::vector<unsigned> annot;
|
2016-12-15 06:32:44 +08:00
|
|
|
if (findAllNVVMAnnotation(func, "rdoimage", annot)) {
|
2016-08-12 06:21:41 +08:00
|
|
|
if (is_contained(annot, arg->getArgNo()))
|
2012-05-05 04:18:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool isImageWriteOnly(const Value &val) {
|
2012-05-05 04:18:50 +08:00
|
|
|
if (const Argument *arg = dyn_cast<Argument>(&val)) {
|
|
|
|
const Function *func = arg->getParent();
|
|
|
|
std::vector<unsigned> annot;
|
2016-12-15 06:32:44 +08:00
|
|
|
if (findAllNVVMAnnotation(func, "wroimage", annot)) {
|
2016-08-12 06:21:41 +08:00
|
|
|
if (is_contained(annot, arg->getArgNo()))
|
2012-05-05 04:18:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool isImageReadWrite(const Value &val) {
|
2014-04-09 23:38:52 +08:00
|
|
|
if (const Argument *arg = dyn_cast<Argument>(&val)) {
|
|
|
|
const Function *func = arg->getParent();
|
|
|
|
std::vector<unsigned> annot;
|
2016-12-15 06:32:44 +08:00
|
|
|
if (findAllNVVMAnnotation(func, "rdwrimage", annot)) {
|
2016-08-12 06:21:41 +08:00
|
|
|
if (is_contained(annot, arg->getArgNo()))
|
2014-04-09 23:38:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool isImage(const Value &val) {
|
|
|
|
return isImageReadOnly(val) || isImageWriteOnly(val) || isImageReadWrite(val);
|
2014-04-09 23:38:52 +08:00
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool isManaged(const Value &val) {
|
2014-04-09 23:38:52 +08:00
|
|
|
if(const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
|
|
|
|
unsigned annot;
|
2016-12-15 06:32:44 +08:00
|
|
|
if (findOneNVVMAnnotation(gv, "managed", annot)) {
|
2014-04-09 23:38:52 +08:00
|
|
|
assert((annot == 1) && "Unexpected annotation on a managed symbol");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2012-05-05 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
std::string getTextureName(const Value &val) {
|
2012-05-05 04:18:50 +08:00
|
|
|
assert(val.hasName() && "Found texture variable with no name");
|
|
|
|
return val.getName();
|
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
std::string getSurfaceName(const Value &val) {
|
2012-05-05 04:18:50 +08:00
|
|
|
assert(val.hasName() && "Found surface variable with no name");
|
|
|
|
return val.getName();
|
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
std::string getSamplerName(const Value &val) {
|
2012-05-05 04:18:50 +08:00
|
|
|
assert(val.hasName() && "Found sampler variable with no name");
|
|
|
|
return val.getName();
|
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool getMaxNTIDx(const Function &F, unsigned &x) {
|
|
|
|
return findOneNVVMAnnotation(&F, "maxntidx", x);
|
2012-05-05 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool getMaxNTIDy(const Function &F, unsigned &y) {
|
|
|
|
return findOneNVVMAnnotation(&F, "maxntidy", y);
|
2012-05-05 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool getMaxNTIDz(const Function &F, unsigned &z) {
|
|
|
|
return findOneNVVMAnnotation(&F, "maxntidz", z);
|
2012-05-05 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool getReqNTIDx(const Function &F, unsigned &x) {
|
|
|
|
return findOneNVVMAnnotation(&F, "reqntidx", x);
|
2012-05-05 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool getReqNTIDy(const Function &F, unsigned &y) {
|
|
|
|
return findOneNVVMAnnotation(&F, "reqntidy", y);
|
2012-05-05 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool getReqNTIDz(const Function &F, unsigned &z) {
|
|
|
|
return findOneNVVMAnnotation(&F, "reqntidz", z);
|
2012-05-05 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool getMinCTASm(const Function &F, unsigned &x) {
|
|
|
|
return findOneNVVMAnnotation(&F, "minctasm", x);
|
2012-05-05 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:50 +08:00
|
|
|
bool getMaxNReg(const Function &F, unsigned &x) {
|
|
|
|
return findOneNVVMAnnotation(&F, "maxnreg", x);
|
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool isKernelFunction(const Function &F) {
|
2012-05-05 04:18:50 +08:00
|
|
|
unsigned x = 0;
|
2016-12-15 06:32:44 +08:00
|
|
|
bool retval = findOneNVVMAnnotation(&F, "kernel", x);
|
2015-03-24 00:26:23 +08:00
|
|
|
if (!retval) {
|
2012-05-05 04:18:50 +08:00
|
|
|
// There is no NVVM metadata, check the calling convention
|
2016-12-15 06:32:44 +08:00
|
|
|
return F.getCallingConv() == CallingConv::PTX_Kernel;
|
2012-05-05 04:18:50 +08:00
|
|
|
}
|
2013-03-30 22:29:21 +08:00
|
|
|
return (x == 1);
|
2012-05-05 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool getAlign(const Function &F, unsigned index, unsigned &align) {
|
2012-05-05 04:18:50 +08:00
|
|
|
std::vector<unsigned> Vs;
|
2016-12-15 06:32:44 +08:00
|
|
|
bool retval = findAllNVVMAnnotation(&F, "align", Vs);
|
2015-03-24 00:26:23 +08:00
|
|
|
if (!retval)
|
2012-05-05 04:18:50 +08:00
|
|
|
return false;
|
2013-03-30 22:29:21 +08:00
|
|
|
for (int i = 0, e = Vs.size(); i < e; i++) {
|
2012-05-05 04:18:50 +08:00
|
|
|
unsigned v = Vs[i];
|
2013-03-30 22:29:21 +08:00
|
|
|
if ((v >> 16) == index) {
|
|
|
|
align = v & 0xFFFF;
|
2012-05-05 04:18:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
bool getAlign(const CallInst &I, unsigned index, unsigned &align) {
|
2014-11-12 05:30:22 +08:00
|
|
|
if (MDNode *alignNode = I.getMetadata("callalign")) {
|
2013-03-30 22:29:21 +08:00
|
|
|
for (int i = 0, n = alignNode->getNumOperands(); i < n; i++) {
|
2012-05-05 04:18:50 +08:00
|
|
|
if (const ConstantInt *CI =
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
mdconst::dyn_extract<ConstantInt>(alignNode->getOperand(i))) {
|
2012-05-05 04:18:50 +08:00
|
|
|
unsigned v = CI->getZExtValue();
|
2013-03-30 22:29:21 +08:00
|
|
|
if ((v >> 16) == index) {
|
2012-05-05 04:18:50 +08:00
|
|
|
align = v & 0xFFFF;
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-30 22:29:21 +08:00
|
|
|
if ((v >> 16) > index) {
|
2012-05-05 04:18:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-15 06:32:44 +08:00
|
|
|
} // namespace llvm
|