clang  19.0.0git
ByteCodeEmitter.cpp
Go to the documentation of this file.
1 //===--- ByteCodeEmitter.cpp - Instruction emitter for the VM ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ByteCodeEmitter.h"
10 #include "Context.h"
11 #include "Floating.h"
12 #include "IntegralAP.h"
13 #include "Opcode.h"
14 #include "Program.h"
15 #include "clang/AST/ASTLambda.h"
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/Basic/Builtins.h"
19 #include <type_traits>
20 
21 using namespace clang;
22 using namespace clang::interp;
23 
24 /// Unevaluated builtins don't get their arguments put on the stack
25 /// automatically. They instead operate on the AST of their Call
26 /// Expression.
27 /// Similar information is available via ASTContext::BuiltinInfo,
28 /// but that is not correct for our use cases.
29 static bool isUnevaluatedBuiltin(unsigned BuiltinID) {
30  return BuiltinID == Builtin::BI__builtin_classify_type;
31 }
32 
34  bool IsLambdaStaticInvoker = false;
35  if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl);
36  MD && MD->isLambdaStaticInvoker()) {
37  // For a lambda static invoker, we might have to pick a specialized
38  // version if the lambda is generic. In that case, the picked function
39  // will *NOT* be a static invoker anymore. However, it will still
40  // be a non-static member function, this (usually) requiring an
41  // instance pointer. We suppress that later in this function.
42  IsLambdaStaticInvoker = true;
43 
44  const CXXRecordDecl *ClosureClass = MD->getParent();
45  assert(ClosureClass->captures_begin() == ClosureClass->captures_end());
46  if (ClosureClass->isGenericLambda()) {
47  const CXXMethodDecl *LambdaCallOp = ClosureClass->getLambdaCallOperator();
48  assert(MD->isFunctionTemplateSpecialization() &&
49  "A generic lambda's static-invoker function must be a "
50  "template specialization");
51  const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs();
52  FunctionTemplateDecl *CallOpTemplate =
53  LambdaCallOp->getDescribedFunctionTemplate();
54  void *InsertPos = nullptr;
55  const FunctionDecl *CorrespondingCallOpSpecialization =
56  CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos);
57  assert(CorrespondingCallOpSpecialization);
58  FuncDecl = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization);
59  }
60  }
61 
62  // Set up argument indices.
63  unsigned ParamOffset = 0;
64  SmallVector<PrimType, 8> ParamTypes;
65  SmallVector<unsigned, 8> ParamOffsets;
66  llvm::DenseMap<unsigned, Function::ParamDescriptor> ParamDescriptors;
67 
68  // If the return is not a primitive, a pointer to the storage where the
69  // value is initialized in is passed as the first argument. See 'RVO'
70  // elsewhere in the code.
71  QualType Ty = FuncDecl->getReturnType();
72  bool HasRVO = false;
73  if (!Ty->isVoidType() && !Ctx.classify(Ty)) {
74  HasRVO = true;
75  ParamTypes.push_back(PT_Ptr);
76  ParamOffsets.push_back(ParamOffset);
78  }
79 
80  // If the function decl is a member decl, the next parameter is
81  // the 'this' pointer. This parameter is pop()ed from the
82  // InterpStack when calling the function.
83  bool HasThisPointer = false;
84  if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl)) {
85  if (!IsLambdaStaticInvoker) {
86  HasThisPointer = MD->isInstance();
87  if (MD->isImplicitObjectMemberFunction()) {
88  ParamTypes.push_back(PT_Ptr);
89  ParamOffsets.push_back(ParamOffset);
91  }
92  }
93 
94  // Set up lambda capture to closure record field mapping.
95  if (isLambdaCallOperator(MD)) {
96  const Record *R = P.getOrCreateRecord(MD->getParent());
97  llvm::DenseMap<const ValueDecl *, FieldDecl *> LC;
98  FieldDecl *LTC;
99 
100  MD->getParent()->getCaptureFields(LC, LTC);
101 
102  for (auto Cap : LC) {
103  // Static lambdas cannot have any captures. If this one does,
104  // it has already been diagnosed and we can only ignore it.
105  if (MD->isStatic())
106  return nullptr;
107 
108  unsigned Offset = R->getField(Cap.second)->Offset;
109  this->LambdaCaptures[Cap.first] = {
110  Offset, Cap.second->getType()->isReferenceType()};
111  }
112  if (LTC) {
113  QualType CaptureType = R->getField(LTC)->Decl->getType();
114  this->LambdaThisCapture = {R->getField(LTC)->Offset,
115  CaptureType->isReferenceType() ||
116  CaptureType->isPointerType()};
117  }
118  }
119  }
120 
121  // Assign descriptors to all parameters.
122  // Composite objects are lowered to pointers.
123  for (const ParmVarDecl *PD : FuncDecl->parameters()) {
124  std::optional<PrimType> T = Ctx.classify(PD->getType());
125  PrimType PT = T.value_or(PT_Ptr);
126  Descriptor *Desc = P.createDescriptor(PD, PT);
127  ParamDescriptors.insert({ParamOffset, {PT, Desc}});
128  Params.insert({PD, {ParamOffset, T != std::nullopt}});
129  ParamOffsets.push_back(ParamOffset);
130  ParamOffset += align(primSize(PT));
131  ParamTypes.push_back(PT);
132  }
133 
134  // Create a handle over the emitted code.
135  Function *Func = P.getFunction(FuncDecl);
136  if (!Func) {
137  bool IsUnevaluatedBuiltin = false;
138  if (unsigned BI = FuncDecl->getBuiltinID())
139  IsUnevaluatedBuiltin = isUnevaluatedBuiltin(BI);
140 
141  Func =
142  P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes),
143  std::move(ParamDescriptors), std::move(ParamOffsets),
144  HasThisPointer, HasRVO, IsUnevaluatedBuiltin);
145  }
146 
147  assert(Func);
148  // For not-yet-defined functions, we only create a Function instance and
149  // compile their body later.
150  if (!FuncDecl->isDefined()) {
151  Func->setDefined(false);
152  return Func;
153  }
154 
155  Func->setDefined(true);
156 
157  // Lambda static invokers are a special case that we emit custom code for.
158  bool IsEligibleForCompilation = false;
159  if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl))
160  IsEligibleForCompilation = MD->isLambdaStaticInvoker();
161  if (!IsEligibleForCompilation)
162  IsEligibleForCompilation =
163  FuncDecl->isConstexpr() || FuncDecl->hasAttr<MSConstexprAttr>();
164 
165  // Compile the function body.
166  if (!IsEligibleForCompilation || !visitFunc(FuncDecl)) {
167  Func->setIsFullyCompiled(true);
168  return Func;
169  }
170 
171  // Create scopes from descriptors.
173  for (auto &DS : Descriptors) {
174  Scopes.emplace_back(std::move(DS));
175  }
176 
177  // Set the function's code.
178  Func->setCode(NextLocalOffset, std::move(Code), std::move(SrcMap),
179  std::move(Scopes), FuncDecl->hasBody());
180  Func->setIsFullyCompiled(true);
181  return Func;
182 }
183 
185  NextLocalOffset += sizeof(Block);
186  unsigned Location = NextLocalOffset;
187  NextLocalOffset += align(D->getAllocSize());
188  return {Location, D};
189 }
190 
192  const size_t Target = Code.size();
193  LabelOffsets.insert({Label, Target});
194 
195  if (auto It = LabelRelocs.find(Label);
196  It != LabelRelocs.end()) {
197  for (unsigned Reloc : It->second) {
198  using namespace llvm::support;
199 
200  // Rewrite the operand of all jumps to this label.
201  void *Location = Code.data() + Reloc - align(sizeof(int32_t));
202  assert(aligned(Location));
203  const int32_t Offset = Target - static_cast<int64_t>(Reloc);
204  endian::write<int32_t, llvm::endianness::native>(Location, Offset);
205  }
206  LabelRelocs.erase(It);
207  }
208 }
209 
210 int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
211  // Compute the PC offset which the jump is relative to.
212  const int64_t Position =
213  Code.size() + align(sizeof(Opcode)) + align(sizeof(int32_t));
214  assert(aligned(Position));
215 
216  // If target is known, compute jump offset.
217  if (auto It = LabelOffsets.find(Label);
218  It != LabelOffsets.end())
219  return It->second - Position;
220 
221  // Otherwise, record relocation and return dummy offset.
222  LabelRelocs[Label].push_back(Position);
223  return 0ull;
224 }
225 
226 /// Helper to write bytecode and bail out if 32-bit offsets become invalid.
227 /// Pointers will be automatically marshalled as 32-bit IDs.
228 template <typename T>
229 static void emit(Program &P, std::vector<std::byte> &Code, const T &Val,
230  bool &Success) {
231  size_t Size;
232 
233  if constexpr (std::is_pointer_v<T>)
234  Size = sizeof(uint32_t);
235  else
236  Size = sizeof(T);
237 
238  if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
239  Success = false;
240  return;
241  }
242 
243  // Access must be aligned!
244  size_t ValPos = align(Code.size());
245  Size = align(Size);
246  assert(aligned(ValPos + Size));
247  Code.resize(ValPos + Size);
248 
249  if constexpr (!std::is_pointer_v<T>) {
250  new (Code.data() + ValPos) T(Val);
251  } else {
252  uint32_t ID = P.getOrCreateNativePointer(Val);
253  new (Code.data() + ValPos) uint32_t(ID);
254  }
255 }
256 
257 /// Emits a serializable value. These usually (potentially) contain
258 /// heap-allocated memory and aren't trivially copyable.
259 template <typename T>
260 static void emitSerialized(std::vector<std::byte> &Code, const T &Val,
261  bool &Success) {
262  size_t Size = Val.bytesToSerialize();
263 
264  if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
265  Success = false;
266  return;
267  }
268 
269  // Access must be aligned!
270  size_t ValPos = align(Code.size());
271  Size = align(Size);
272  assert(aligned(ValPos + Size));
273  Code.resize(ValPos + Size);
274 
275  Val.serialize(Code.data() + ValPos);
276 }
277 
278 template <>
279 void emit(Program &P, std::vector<std::byte> &Code, const Floating &Val,
280  bool &Success) {
281  emitSerialized(Code, Val, Success);
282 }
283 
284 template <>
285 void emit(Program &P, std::vector<std::byte> &Code,
286  const IntegralAP<false> &Val, bool &Success) {
287  emitSerialized(Code, Val, Success);
288 }
289 
290 template <>
291 void emit(Program &P, std::vector<std::byte> &Code, const IntegralAP<true> &Val,
292  bool &Success) {
293  emitSerialized(Code, Val, Success);
294 }
295 
296 template <typename... Tys>
297 bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &... Args, const SourceInfo &SI) {
298  bool Success = true;
299 
300  // The opcode is followed by arguments. The source info is
301  // attached to the address after the opcode.
302  emit(P, Code, Op, Success);
303  if (SI)
304  SrcMap.emplace_back(Code.size(), SI);
305 
306  (..., emit(P, Code, Args, Success));
307  return Success;
308 }
309 
311  return emitJt(getOffset(Label), SourceInfo{});
312 }
313 
315  return emitJf(getOffset(Label), SourceInfo{});
316 }
317 
319  return emitJmp(getOffset(Label), SourceInfo{});
320 }
321 
323  emitLabel(Label);
324  return true;
325 }
326 
327 //===----------------------------------------------------------------------===//
328 // Opcode emitters
329 //===----------------------------------------------------------------------===//
330 
331 #define GET_LINK_IMPL
332 #include "Opcodes.inc"
333 #undef GET_LINK_IMPL
This file provides some common utility functions for processing Lambda related AST Constructs.
StringRef P
static char ID
Definition: Arena.cpp:183
Defines enum values for all the target-independent builtin functions.
static bool isUnevaluatedBuiltin(unsigned BuiltinID)
Unevaluated builtins don't get their arguments put on the stack automatically.
static void emit(Program &P, std::vector< std::byte > &Code, const T &Val, bool &Success)
Helper to write bytecode and bail out if 32-bit offsets become invalid.
static void emitSerialized(std::vector< std::byte > &Code, const T &Val, bool &Success)
Emits a serializable value.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
unsigned Offset
Definition: Format.cpp:2978
llvm::MachO::Target Target
Definition: MachO.h:50
std::string Label
__DEVICE__ int max(int __a, int __b)
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2060
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
bool isGenericLambda() const
Determine whether this class describes a generic lambda function object (i.e.
Definition: DeclCXX.cpp:1564
capture_const_iterator captures_end() const
Definition: DeclCXX.h:1111
capture_const_iterator captures_begin() const
Definition: DeclCXX.h:1105
CXXMethodDecl * getLambdaCallOperator() const
Retrieve the lambda call operator of the closure type if this is a closure type.
Definition: DeclCXX.cpp:1594
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:2066
bool hasAttr() const
Definition: DeclBase.h:583
Represents a member of a struct/union/class.
Definition: Decl.h:3060
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition: Decl.h:3273
Represents a function declaration or definition.
Definition: Decl.h:1972
FunctionTemplateDecl * getDescribedFunctionTemplate() const
Retrieves the function template that is described by this function declaration.
Definition: Decl.cpp:4046
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:3636
QualType getReturnType() const
Definition: Decl.h:2757
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
Definition: Decl.h:2435
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:2686
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition: Decl.cpp:3160
bool isDefined(const FunctionDecl *&Definition, bool CheckForPendingFriendDefinition=false) const
Returns true if the function has a definition that does not need to be instantiated.
Definition: Decl.cpp:3207
Declaration of a template function.
Definition: DeclTemplate.h:957
FunctionDecl * findSpecialization(ArrayRef< TemplateArgument > Args, void *&InsertPos)
Return the specialization with the provided arguments if it exists, otherwise return the insertion po...
Represents a parameter to a function.
Definition: Decl.h:1762
A (possibly-)qualified type.
Definition: Type.h:940
A template argument list.
Definition: DeclTemplate.h:244
ArrayRef< TemplateArgument > asArray() const
Produce this as an array ref.
Definition: DeclTemplate.h:274
bool isVoidType() const
Definition: Type.h:7939
bool isPointerType() const
Definition: Type.h:7624
bool isReferenceType() const
Definition: Type.h:7636
QualType getType() const
Definition: Decl.h:718
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
bool jump(const LabelTy &Label)
void emitLabel(LabelTy Label)
Define a label.
ParamOffset LambdaThisCapture
Offset of the This parameter in a lambda record.
llvm::DenseMap< const ParmVarDecl *, ParamOffset > Params
Parameter indices.
llvm::DenseMap< const ValueDecl *, ParamOffset > LambdaCaptures
Lambda captures.
bool fallthrough(const LabelTy &Label)
Local createLocal(Descriptor *D)
Callback for local registration.
Function * compileFunc(const FunctionDecl *FuncDecl)
Compiles the function into the module.
virtual bool visitFunc(const FunctionDecl *E)=0
Methods implemented by the compiler.
bool jumpTrue(const LabelTy &Label)
Emits jumps.
bool jumpFalse(const LabelTy &Label)
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
std::optional< PrimType > classify(QualType T) const
Classifies a type.
Definition: Context.cpp:119
Bytecode function.
Definition: Function.h:77
The program contains and links the bytecode for all functions.
Definition: Program.h:39
Function * getFunction(const FunctionDecl *F)
Returns a function.
Definition: Program.cpp:233
Descriptor * createDescriptor(const DeclTy &D, PrimType Type, Descriptor::MetadataSize MDSize=std::nullopt, bool IsConst=false, bool IsTemporary=false, bool IsMutable=false)
Creates a descriptor for a primitive type.
Definition: Program.h:116
Function * createFunction(const FunctionDecl *Def, Ts &&... Args)
Creates a new function from a code range.
Definition: Program.h:95
Record * getOrCreateRecord(const RecordDecl *RD)
Returns a record or creates one if it does not exist.
Definition: Program.cpp:240
Structure/Class descriptor.
Definition: Record.h:25
const Field * getField(const FieldDecl *FD) const
Returns a field.
Definition: Record.cpp:39
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:72
constexpr bool aligned(uintptr_t Value)
Definition: PrimType.h:103
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:99
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:32
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition: PrimType.cpp:22
The JSON file list parser is used to communicate input to InstallAPI.
bool isLambdaCallOperator(const CXXMethodDecl *MD)
Definition: ASTLambda.h:27
const FunctionProtoType * T
long int64_t
Describes a memory block created by an allocation site.
Definition: Descriptor.h:91
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition: Descriptor.h:204
const FieldDecl * Decl
Definition: Record.h:29
Information about a local's storage.
Definition: Function.h:38