clang  6.0.0svn
NVPTX.cpp
Go to the documentation of this file.
1 //===--- NVPTX.cpp - Implement NVPTX target feature support ---------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements NVPTX TargetInfo objects.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "NVPTX.h"
15 #include "Targets.h"
16 #include "clang/Basic/Builtins.h"
19 #include "llvm/ADT/StringSwitch.h"
20 
21 using namespace clang;
22 using namespace clang::targets;
23 
24 const Builtin::Info NVPTXTargetInfo::BuiltinInfo[] = {
25 #define BUILTIN(ID, TYPE, ATTRS) \
26  {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
27 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \
28  {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
29 #define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \
30  {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE},
31 #include "clang/Basic/BuiltinsNVPTX.def"
32 };
33 
34 const char *const NVPTXTargetInfo::GCCRegNames[] = {"r0"};
35 
36 NVPTXTargetInfo::NVPTXTargetInfo(const llvm::Triple &Triple,
37  const TargetOptions &Opts,
38  unsigned TargetPointerWidth)
39  : TargetInfo(Triple) {
40  assert((TargetPointerWidth == 32 || TargetPointerWidth == 64) &&
41  "NVPTX only supports 32- and 64-bit modes.");
42 
43  TLSSupported = false;
44  VLASupported = false;
47 
48  // Define available target features
49  // These must be defined in sorted order!
50  NoAsmVariants = true;
51  GPU = CudaArch::SM_20;
52 
53  if (TargetPointerWidth == 32)
54  resetDataLayout("e-p:32:32-i64:64-i128:128-v16:16-v32:32-n16:32:64");
55  else
56  resetDataLayout("e-i64:64-i128:128-v16:16-v32:32-n16:32:64");
57 
58  // If possible, get a TargetInfo for our host triple, so we can match its
59  // types.
60  llvm::Triple HostTriple(Opts.HostTriple);
61  if (!HostTriple.isNVPTX())
62  HostTarget.reset(AllocateTarget(llvm::Triple(Opts.HostTriple), Opts));
63 
64  // If no host target, make some guesses about the data layout and return.
65  if (!HostTarget) {
66  LongWidth = LongAlign = TargetPointerWidth;
67  PointerWidth = PointerAlign = TargetPointerWidth;
68  switch (TargetPointerWidth) {
69  case 32:
73  break;
74  case 64:
78  break;
79  default:
80  llvm_unreachable("TargetPointerWidth must be 32 or 64");
81  }
82  return;
83  }
84 
85  // Copy properties from host target.
86  PointerWidth = HostTarget->getPointerWidth(/* AddrSpace = */ 0);
87  PointerAlign = HostTarget->getPointerAlign(/* AddrSpace = */ 0);
88  BoolWidth = HostTarget->getBoolWidth();
89  BoolAlign = HostTarget->getBoolAlign();
90  IntWidth = HostTarget->getIntWidth();
91  IntAlign = HostTarget->getIntAlign();
92  HalfWidth = HostTarget->getHalfWidth();
93  HalfAlign = HostTarget->getHalfAlign();
94  FloatWidth = HostTarget->getFloatWidth();
95  FloatAlign = HostTarget->getFloatAlign();
96  DoubleWidth = HostTarget->getDoubleWidth();
97  DoubleAlign = HostTarget->getDoubleAlign();
98  LongWidth = HostTarget->getLongWidth();
99  LongAlign = HostTarget->getLongAlign();
100  LongLongWidth = HostTarget->getLongLongWidth();
101  LongLongAlign = HostTarget->getLongLongAlign();
102  MinGlobalAlign = HostTarget->getMinGlobalAlign();
103  NewAlign = HostTarget->getNewAlign();
105  HostTarget->getDefaultAlignForAttributeAligned();
106  SizeType = HostTarget->getSizeType();
107  IntMaxType = HostTarget->getIntMaxType();
108  PtrDiffType = HostTarget->getPtrDiffType(/* AddrSpace = */ 0);
109  IntPtrType = HostTarget->getIntPtrType();
110  WCharType = HostTarget->getWCharType();
111  WIntType = HostTarget->getWIntType();
112  Char16Type = HostTarget->getChar16Type();
113  Char32Type = HostTarget->getChar32Type();
114  Int64Type = HostTarget->getInt64Type();
115  SigAtomicType = HostTarget->getSigAtomicType();
116  ProcessIDType = HostTarget->getProcessIDType();
117 
118  UseBitFieldTypeAlignment = HostTarget->useBitFieldTypeAlignment();
119  UseZeroLengthBitfieldAlignment = HostTarget->useZeroLengthBitfieldAlignment();
120  UseExplicitBitFieldAlignment = HostTarget->useExplicitBitFieldAlignment();
121  ZeroLengthBitfieldBoundary = HostTarget->getZeroLengthBitfieldBoundary();
122 
123  // This is a bit of a lie, but it controls __GCC_ATOMIC_XXX_LOCK_FREE, and
124  // we need those macros to be identical on host and device, because (among
125  // other things) they affect which standard library classes are defined, and
126  // we need all classes to be defined on both the host and device.
127  MaxAtomicInlineWidth = HostTarget->getMaxAtomicInlineWidth();
128 
129  // Properties intentionally not copied from host:
130  // - LargeArrayMinWidth, LargeArrayAlign: Not visible across the
131  // host/device boundary.
132  // - SuitableAlign: Not visible across the host/device boundary, and may
133  // correctly be different on host/device, e.g. if host has wider vector
134  // types than device.
135  // - LongDoubleWidth, LongDoubleAlign: nvptx's long double type is the same
136  // as its double type, but that's not necessarily true on the host.
137  // TODO: nvcc emits a warning when using long double on device; we should
138  // do the same.
139 }
140 
142  return llvm::makeArrayRef(GCCRegNames);
143 }
144 
145 bool NVPTXTargetInfo::hasFeature(StringRef Feature) const {
146  return llvm::StringSwitch<bool>(Feature)
147  .Cases("ptx", "nvptx", true)
148  .Case("satom", GPU >= CudaArch::SM_60) // Atomics w/ scope.
149  .Default(false);
150 }
151 
153  MacroBuilder &Builder) const {
154  Builder.defineMacro("__PTX__");
155  Builder.defineMacro("__NVPTX__");
156  if (Opts.CUDAIsDevice) {
157  // Set __CUDA_ARCH__ for the GPU specified.
158  std::string CUDAArchCode = [this] {
159  switch (GPU) {
160  case CudaArch::UNKNOWN:
161  assert(false && "No GPU arch when compiling CUDA device code.");
162  return "";
163  case CudaArch::SM_20:
164  return "200";
165  case CudaArch::SM_21:
166  return "210";
167  case CudaArch::SM_30:
168  return "300";
169  case CudaArch::SM_32:
170  return "320";
171  case CudaArch::SM_35:
172  return "350";
173  case CudaArch::SM_37:
174  return "370";
175  case CudaArch::SM_50:
176  return "500";
177  case CudaArch::SM_52:
178  return "520";
179  case CudaArch::SM_53:
180  return "530";
181  case CudaArch::SM_60:
182  return "600";
183  case CudaArch::SM_61:
184  return "610";
185  case CudaArch::SM_62:
186  return "620";
187  case CudaArch::SM_70:
188  return "700";
189  }
190  llvm_unreachable("unhandled CudaArch");
191  }();
192  Builder.defineMacro("__CUDA_ARCH__", CUDAArchCode);
193  }
194 }
195 
197  return llvm::makeArrayRef(BuiltinInfo, clang::NVPTX::LastTSBuiltin -
199 }
Defines the clang::MacroBuilder utility class.
IntType IntPtrType
Definition: TargetInfo.h:192
unsigned char IntWidth
Definition: TargetInfo.h:67
unsigned char LongLongWidth
Definition: TargetInfo.h:74
IntType SigAtomicType
Definition: TargetInfo.h:192
unsigned char FloatAlign
Definition: TargetInfo.h:69
Options for controlling the target.
Definition: TargetOptions.h:26
std::string HostTriple
When compiling for the device side, contains the triple used to compile for the host.
Definition: TargetOptions.h:33
ArrayRef< Builtin::Info > getTargetBuiltins() const override
Return information about target-specific builtins for the current primary target, and info about whic...
Definition: NVPTX.cpp:196
unsigned ZeroLengthBitfieldBoundary
If non-zero, specifies a fixed alignment value for bitfields that follow zero length bitfield...
Definition: TargetInfo.h:222
unsigned char MinGlobalAlign
Definition: TargetInfo.h:77
ArrayRef< const char * > getGCCRegNames() const override
Definition: NVPTX.cpp:141
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
unsigned char DefaultAlignForAttributeAligned
Definition: TargetInfo.h:76
unsigned char PointerWidth
Definition: TargetInfo.h:65
unsigned char FloatWidth
Definition: TargetInfo.h:69
NVPTXTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts, unsigned TargetPointerWidth)
Definition: NVPTX.cpp:36
unsigned char DoubleWidth
Definition: TargetInfo.h:70
unsigned char LongWidth
Definition: TargetInfo.h:73
bool hasFeature(StringRef Feature) const override
Determine whether the given target has the given feature.
Definition: NVPTX.cpp:145
Exposes information about the current target.
Definition: TargetInfo.h:54
IntType Char32Type
Definition: TargetInfo.h:192
unsigned short NewAlign
Definition: TargetInfo.h:82
void resetDataLayout(StringRef DL)
Definition: TargetInfo.h:105
unsigned UseExplicitBitFieldAlignment
Whether explicit bit field alignment attributes are honored.
Definition: TargetInfo.h:218
TargetInfo * AllocateTarget(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: Targets.cpp:117
IntType PtrDiffType
Definition: TargetInfo.h:192
Enumerates target-specific builtins in their own namespaces within namespace clang.
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods --------------------——===//
Definition: NVPTX.cpp:152
const LangASMap * AddrSpaceMap
Definition: TargetInfo.h:89
unsigned char DoubleAlign
Definition: TargetInfo.h:70
unsigned UseZeroLengthBitfieldAlignment
Whether zero length bitfields (e.g., int : 0;) force alignment of the next bitfield.
Definition: TargetInfo.h:215
unsigned char PointerAlign
Definition: TargetInfo.h:65
Dataflow Directional Tag Classes.
unsigned char BoolWidth
Definition: TargetInfo.h:66
unsigned char LongAlign
Definition: TargetInfo.h:73
unsigned char IntAlign
Definition: TargetInfo.h:67
IntType ProcessIDType
Definition: TargetInfo.h:192
unsigned char LongLongAlign
Definition: TargetInfo.h:74
static const unsigned NVPTXAddrSpaceMap[]
Definition: NVPTX.h:26
IntType Char16Type
Definition: TargetInfo.h:192
unsigned char HalfAlign
Definition: TargetInfo.h:68
unsigned char MaxAtomicInlineWidth
Definition: TargetInfo.h:78
void defineMacro(const Twine &Name, const Twine &Value="1")
Append a #define line for macro of the form "\#define Name Value\n".
Definition: MacroBuilder.h:30
IntType IntMaxType
Definition: TargetInfo.h:192
unsigned UseBitFieldTypeAlignment
Control whether the alignment of bit-field types is respected when laying out structures.
Definition: TargetInfo.h:207
Defines enum values for all the target-independent builtin functions.
unsigned char BoolAlign
Definition: TargetInfo.h:66
unsigned char HalfWidth
Definition: TargetInfo.h:68
bool UseAddrSpaceMapMangling
Specify if mangling based on address space map should be used or not for language specific address sp...
Definition: TargetInfo.h:226