1 //===- Action.h - Abstract compilation steps --------------------*- 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 #ifndef LLVM_CLANG_DRIVER_ACTION_H 10 #define LLVM_CLANG_DRIVER_ACTION_H 11 12 #include "clang/Basic/LLVM.h" 13 #include "clang/Driver/Types.h" 14 #include "clang/Driver/Util.h" 15 #include "llvm/ADT/ArrayRef.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/ADT/iterator_range.h" 20 #include <string> 21 22 namespace llvm { 23 namespace opt { 24 25 class Arg; 26 27 } // namespace opt 28 } // namespace llvm 29 30 namespace clang { 31 namespace driver { 32 33 class ToolChain; 34 35 /// Action - Represent an abstract compilation step to perform. 36 /// 37 /// An action represents an edge in the compilation graph; typically 38 /// it is a job to transform an input using some tool. 39 /// 40 /// The current driver is hard wired to expect actions which produce a 41 /// single primary output, at least in terms of controlling the 42 /// compilation. Actions can produce auxiliary files, but can only 43 /// produce a single output to feed into subsequent actions. 44 /// 45 /// Actions are usually owned by a Compilation, which creates new 46 /// actions via MakeAction(). 47 class Action { 48 public: 49 using size_type = ActionList::size_type; 50 using input_iterator = ActionList::iterator; 51 using input_const_iterator = ActionList::const_iterator; 52 using input_range = llvm::iterator_range<input_iterator>; 53 using input_const_range = llvm::iterator_range<input_const_iterator>; 54 55 enum ActionClass { 56 InputClass = 0, 57 BindArchClass, 58 OffloadClass, 59 PreprocessJobClass, 60 PrecompileJobClass, 61 ExtractAPIJobClass, 62 AnalyzeJobClass, 63 MigrateJobClass, 64 CompileJobClass, 65 BackendJobClass, 66 AssembleJobClass, 67 LinkJobClass, 68 IfsMergeJobClass, 69 LipoJobClass, 70 DsymutilJobClass, 71 VerifyDebugInfoJobClass, 72 VerifyPCHJobClass, 73 OffloadBundlingJobClass, 74 OffloadUnbundlingJobClass, 75 OffloadPackagerJobClass, 76 LinkerWrapperJobClass, 77 StaticLibJobClass, 78 BinaryAnalyzeJobClass, 79 80 JobClassFirst = PreprocessJobClass, 81 JobClassLast = BinaryAnalyzeJobClass 82 }; 83 84 // The offloading kind determines if this action is binded to a particular 85 // programming model. Each entry reserves one bit. We also have a special kind 86 // to designate the host offloading tool chain. 87 enum OffloadKind { 88 OFK_None = 0x00, 89 90 // The host offloading tool chain. 91 OFK_Host = 0x01, 92 93 // The device offloading tool chains - one bit for each programming model. 94 OFK_Cuda = 0x02, 95 OFK_OpenMP = 0x04, 96 OFK_HIP = 0x08, 97 OFK_SYCL = 0x10, 98 }; 99 100 static const char *getClassName(ActionClass AC); 101 102 private: 103 ActionClass Kind; 104 105 /// The output type of this action. 106 types::ID Type; 107 108 ActionList Inputs; 109 110 /// Flag that is set to true if this action can be collapsed with others 111 /// actions that depend on it. This is true by default and set to false when 112 /// the action is used by two different tool chains, which is enabled by the 113 /// offloading support implementation. 114 bool CanBeCollapsedWithNextDependentAction = true; 115 116 protected: 117 /// 118 /// Offload information. 119 /// 120 121 /// The host offloading kind - a combination of kinds encoded in a mask. 122 /// Multiple programming models may be supported simultaneously by the same 123 /// host. 124 unsigned ActiveOffloadKindMask = 0u; 125 126 /// Offloading kind of the device. 127 OffloadKind OffloadingDeviceKind = OFK_None; 128 129 /// The Offloading architecture associated with this action. 130 const char *OffloadingArch = nullptr; 131 132 /// The Offloading toolchain associated with this device action. 133 const ToolChain *OffloadingToolChain = nullptr; 134 135 Action(ActionClass Kind, types::ID Type) : Action(Kind, ActionList(), Type) {} 136 Action(ActionClass Kind, Action *Input, types::ID Type) 137 : Action(Kind, ActionList({Input}), Type) {} 138 Action(ActionClass Kind, Action *Input) 139 : Action(Kind, ActionList({Input}), Input->getType()) {} 140 Action(ActionClass Kind, const ActionList &Inputs, types::ID Type) 141 : Kind(Kind), Type(Type), Inputs(Inputs) {} 142 143 public: 144 virtual ~Action(); 145 146 const char *getClassName() const { return Action::getClassName(getKind()); } 147 148 ActionClass getKind() const { return Kind; } 149 types::ID getType() const { return Type; } 150 151 ActionList &getInputs() { return Inputs; } 152 const ActionList &getInputs() const { return Inputs; } 153 154 size_type size() const { return Inputs.size(); } 155 156 input_iterator input_begin() { return Inputs.begin(); } 157 input_iterator input_end() { return Inputs.end(); } 158 input_range inputs() { return input_range(input_begin(), input_end()); } 159 input_const_iterator input_begin() const { return Inputs.begin(); } 160 input_const_iterator input_end() const { return Inputs.end(); } 161 input_const_range inputs() const { 162 return input_const_range(input_begin(), input_end()); 163 } 164 165 /// Mark this action as not legal to collapse. 166 void setCannotBeCollapsedWithNextDependentAction() { 167 CanBeCollapsedWithNextDependentAction = false; 168 } 169 170 /// Return true if this function can be collapsed with others. 171 bool isCollapsingWithNextDependentActionLegal() const { 172 return CanBeCollapsedWithNextDependentAction; 173 } 174 175 /// Return a string containing the offload kind of the action. 176 std::string getOffloadingKindPrefix() const; 177 178 /// Return a string that can be used as prefix in order to generate unique 179 /// files for each offloading kind. By default, no prefix is used for 180 /// non-device kinds, except if \a CreatePrefixForHost is set. 181 static std::string 182 GetOffloadingFileNamePrefix(OffloadKind Kind, 183 StringRef NormalizedTriple, 184 bool CreatePrefixForHost = false); 185 186 /// Return a string containing a offload kind name. 187 static StringRef GetOffloadKindName(OffloadKind Kind); 188 189 /// Set the device offload info of this action and propagate it to its 190 /// dependences. 191 void propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch, 192 const ToolChain *OToolChain); 193 194 /// Append the host offload info of this action and propagate it to its 195 /// dependences. 196 void propagateHostOffloadInfo(unsigned OKinds, const char *OArch); 197 198 void setHostOffloadInfo(unsigned OKinds, const char *OArch) { 199 ActiveOffloadKindMask |= OKinds; 200 OffloadingArch = OArch; 201 } 202 203 /// Set the offload info of this action to be the same as the provided action, 204 /// and propagate it to its dependences. 205 void propagateOffloadInfo(const Action *A); 206 207 unsigned getOffloadingHostActiveKinds() const { 208 return ActiveOffloadKindMask; 209 } 210 211 OffloadKind getOffloadingDeviceKind() const { return OffloadingDeviceKind; } 212 const char *getOffloadingArch() const { return OffloadingArch; } 213 const ToolChain *getOffloadingToolChain() const { 214 return OffloadingToolChain; 215 } 216 217 /// Check if this action have any offload kinds. Note that host offload kinds 218 /// are only set if the action is a dependence to a host offload action. 219 bool isHostOffloading(unsigned int OKind) const { 220 return ActiveOffloadKindMask & OKind; 221 } 222 bool isDeviceOffloading(OffloadKind OKind) const { 223 return OffloadingDeviceKind == OKind; 224 } 225 bool isOffloading(OffloadKind OKind) const { 226 return isHostOffloading(OKind) || isDeviceOffloading(OKind); 227 } 228 }; 229 230 class InputAction : public Action { 231 const llvm::opt::Arg &Input; 232 std::string Id; 233 virtual void anchor(); 234 235 public: 236 InputAction(const llvm::opt::Arg &Input, types::ID Type, 237 StringRef Id = StringRef()); 238 239 const llvm::opt::Arg &getInputArg() const { return Input; } 240 241 void setId(StringRef _Id) { Id = _Id.str(); } 242 StringRef getId() const { return Id; } 243 244 static bool classof(const Action *A) { 245 return A->getKind() == InputClass; 246 } 247 }; 248 249 class BindArchAction : public Action { 250 virtual void anchor(); 251 252 /// The architecture to bind, or 0 if the default architecture 253 /// should be bound. 254 StringRef ArchName; 255 256 public: 257 BindArchAction(Action *Input, StringRef ArchName); 258 259 StringRef getArchName() const { return ArchName; } 260 261 static bool classof(const Action *A) { 262 return A->getKind() == BindArchClass; 263 } 264 }; 265 266 /// An offload action combines host or/and device actions according to the 267 /// programming model implementation needs and propagates the offloading kind to 268 /// its dependences. 269 class OffloadAction final : public Action { 270 virtual void anchor(); 271 272 public: 273 /// Type used to communicate device actions. It associates bound architecture, 274 /// toolchain, and offload kind to each action. 275 class DeviceDependences final { 276 public: 277 using ToolChainList = SmallVector<const ToolChain *, 3>; 278 using BoundArchList = SmallVector<const char *, 3>; 279 using OffloadKindList = SmallVector<OffloadKind, 3>; 280 281 private: 282 // Lists that keep the information for each dependency. All the lists are 283 // meant to be updated in sync. We are adopting separate lists instead of a 284 // list of structs, because that simplifies forwarding the actions list to 285 // initialize the inputs of the base Action class. 286 287 /// The dependence actions. 288 ActionList DeviceActions; 289 290 /// The offloading toolchains that should be used with the action. 291 ToolChainList DeviceToolChains; 292 293 /// The architectures that should be used with this action. 294 BoundArchList DeviceBoundArchs; 295 296 /// The offload kind of each dependence. 297 OffloadKindList DeviceOffloadKinds; 298 299 public: 300 /// Add an action along with the associated toolchain, bound arch, and 301 /// offload kind. 302 void add(Action &A, const ToolChain &TC, const char *BoundArch, 303 OffloadKind OKind); 304 305 /// Add an action along with the associated toolchain, bound arch, and 306 /// offload kinds. 307 void add(Action &A, const ToolChain &TC, const char *BoundArch, 308 unsigned OffloadKindMask); 309 310 /// Get each of the individual arrays. 311 const ActionList &getActions() const { return DeviceActions; } 312 const ToolChainList &getToolChains() const { return DeviceToolChains; } 313 const BoundArchList &getBoundArchs() const { return DeviceBoundArchs; } 314 const OffloadKindList &getOffloadKinds() const { 315 return DeviceOffloadKinds; 316 } 317 }; 318 319 /// Type used to communicate host actions. It associates bound architecture, 320 /// toolchain, and offload kinds to the host action. 321 class HostDependence final { 322 /// The dependence action. 323 Action &HostAction; 324 325 /// The offloading toolchain that should be used with the action. 326 const ToolChain &HostToolChain; 327 328 /// The architectures that should be used with this action. 329 const char *HostBoundArch = nullptr; 330 331 /// The offload kind of each dependence. 332 unsigned HostOffloadKinds = 0u; 333 334 public: 335 HostDependence(Action &A, const ToolChain &TC, const char *BoundArch, 336 const unsigned OffloadKinds) 337 : HostAction(A), HostToolChain(TC), HostBoundArch(BoundArch), 338 HostOffloadKinds(OffloadKinds) {} 339 340 /// Constructor version that obtains the offload kinds from the device 341 /// dependencies. 342 HostDependence(Action &A, const ToolChain &TC, const char *BoundArch, 343 const DeviceDependences &DDeps); 344 Action *getAction() const { return &HostAction; } 345 const ToolChain *getToolChain() const { return &HostToolChain; } 346 const char *getBoundArch() const { return HostBoundArch; } 347 unsigned getOffloadKinds() const { return HostOffloadKinds; } 348 }; 349 350 using OffloadActionWorkTy = 351 llvm::function_ref<void(Action *, const ToolChain *, const char *)>; 352 353 private: 354 /// The host offloading toolchain that should be used with the action. 355 const ToolChain *HostTC = nullptr; 356 357 /// The tool chains associated with the list of actions. 358 DeviceDependences::ToolChainList DevToolChains; 359 360 public: 361 OffloadAction(const HostDependence &HDep); 362 OffloadAction(const DeviceDependences &DDeps, types::ID Ty); 363 OffloadAction(const HostDependence &HDep, const DeviceDependences &DDeps); 364 365 /// Execute the work specified in \a Work on the host dependence. 366 void doOnHostDependence(const OffloadActionWorkTy &Work) const; 367 368 /// Execute the work specified in \a Work on each device dependence. 369 void doOnEachDeviceDependence(const OffloadActionWorkTy &Work) const; 370 371 /// Execute the work specified in \a Work on each dependence. 372 void doOnEachDependence(const OffloadActionWorkTy &Work) const; 373 374 /// Execute the work specified in \a Work on each host or device dependence if 375 /// \a IsHostDependenceto is true or false, respectively. 376 void doOnEachDependence(bool IsHostDependence, 377 const OffloadActionWorkTy &Work) const; 378 379 /// Return true if the action has a host dependence. 380 bool hasHostDependence() const; 381 382 /// Return the host dependence of this action. This function is only expected 383 /// to be called if the host dependence exists. 384 Action *getHostDependence() const; 385 386 /// Return true if the action has a single device dependence. If \a 387 /// DoNotConsiderHostActions is set, ignore the host dependence, if any, while 388 /// accounting for the number of dependences. 389 bool hasSingleDeviceDependence(bool DoNotConsiderHostActions = false) const; 390 391 /// Return the single device dependence of this action. This function is only 392 /// expected to be called if a single device dependence exists. If \a 393 /// DoNotConsiderHostActions is set, a host dependence is allowed. 394 Action * 395 getSingleDeviceDependence(bool DoNotConsiderHostActions = false) const; 396 397 static bool classof(const Action *A) { return A->getKind() == OffloadClass; } 398 }; 399 400 class JobAction : public Action { 401 virtual void anchor(); 402 403 protected: 404 JobAction(ActionClass Kind, Action *Input, types::ID Type); 405 JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type); 406 407 public: 408 static bool classof(const Action *A) { 409 return (A->getKind() >= JobClassFirst && 410 A->getKind() <= JobClassLast); 411 } 412 }; 413 414 class PreprocessJobAction : public JobAction { 415 void anchor() override; 416 417 public: 418 PreprocessJobAction(Action *Input, types::ID OutputType); 419 420 static bool classof(const Action *A) { 421 return A->getKind() == PreprocessJobClass; 422 } 423 }; 424 425 class PrecompileJobAction : public JobAction { 426 void anchor() override; 427 428 protected: 429 PrecompileJobAction(ActionClass Kind, Action *Input, types::ID OutputType); 430 431 public: 432 PrecompileJobAction(Action *Input, types::ID OutputType); 433 434 static bool classof(const Action *A) { 435 return A->getKind() == PrecompileJobClass; 436 } 437 }; 438 439 class ExtractAPIJobAction : public JobAction { 440 void anchor() override; 441 442 public: 443 ExtractAPIJobAction(Action *Input, types::ID OutputType); 444 445 static bool classof(const Action *A) { 446 return A->getKind() == ExtractAPIJobClass; 447 } 448 449 void addHeaderInput(Action *Input) { getInputs().push_back(Input); } 450 }; 451 452 class AnalyzeJobAction : public JobAction { 453 void anchor() override; 454 455 public: 456 AnalyzeJobAction(Action *Input, types::ID OutputType); 457 458 static bool classof(const Action *A) { 459 return A->getKind() == AnalyzeJobClass; 460 } 461 }; 462 463 class MigrateJobAction : public JobAction { 464 void anchor() override; 465 466 public: 467 MigrateJobAction(Action *Input, types::ID OutputType); 468 469 static bool classof(const Action *A) { 470 return A->getKind() == MigrateJobClass; 471 } 472 }; 473 474 class CompileJobAction : public JobAction { 475 void anchor() override; 476 477 public: 478 CompileJobAction(Action *Input, types::ID OutputType); 479 480 static bool classof(const Action *A) { 481 return A->getKind() == CompileJobClass; 482 } 483 }; 484 485 class BackendJobAction : public JobAction { 486 void anchor() override; 487 488 public: 489 BackendJobAction(Action *Input, types::ID OutputType); 490 491 static bool classof(const Action *A) { 492 return A->getKind() == BackendJobClass; 493 } 494 }; 495 496 class AssembleJobAction : public JobAction { 497 void anchor() override; 498 499 public: 500 AssembleJobAction(Action *Input, types::ID OutputType); 501 502 static bool classof(const Action *A) { 503 return A->getKind() == AssembleJobClass; 504 } 505 }; 506 507 class IfsMergeJobAction : public JobAction { 508 void anchor() override; 509 510 public: 511 IfsMergeJobAction(ActionList &Inputs, types::ID Type); 512 513 static bool classof(const Action *A) { 514 return A->getKind() == IfsMergeJobClass; 515 } 516 }; 517 518 class LinkJobAction : public JobAction { 519 void anchor() override; 520 521 public: 522 LinkJobAction(ActionList &Inputs, types::ID Type); 523 524 static bool classof(const Action *A) { 525 return A->getKind() == LinkJobClass; 526 } 527 }; 528 529 class LipoJobAction : public JobAction { 530 void anchor() override; 531 532 public: 533 LipoJobAction(ActionList &Inputs, types::ID Type); 534 535 static bool classof(const Action *A) { 536 return A->getKind() == LipoJobClass; 537 } 538 }; 539 540 class DsymutilJobAction : public JobAction { 541 void anchor() override; 542 543 public: 544 DsymutilJobAction(ActionList &Inputs, types::ID Type); 545 546 static bool classof(const Action *A) { 547 return A->getKind() == DsymutilJobClass; 548 } 549 }; 550 551 class VerifyJobAction : public JobAction { 552 void anchor() override; 553 554 public: 555 VerifyJobAction(ActionClass Kind, Action *Input, types::ID Type); 556 557 static bool classof(const Action *A) { 558 return A->getKind() == VerifyDebugInfoJobClass || 559 A->getKind() == VerifyPCHJobClass; 560 } 561 }; 562 563 class VerifyDebugInfoJobAction : public VerifyJobAction { 564 void anchor() override; 565 566 public: 567 VerifyDebugInfoJobAction(Action *Input, types::ID Type); 568 569 static bool classof(const Action *A) { 570 return A->getKind() == VerifyDebugInfoJobClass; 571 } 572 }; 573 574 class VerifyPCHJobAction : public VerifyJobAction { 575 void anchor() override; 576 577 public: 578 VerifyPCHJobAction(Action *Input, types::ID Type); 579 580 static bool classof(const Action *A) { 581 return A->getKind() == VerifyPCHJobClass; 582 } 583 }; 584 585 class OffloadBundlingJobAction : public JobAction { 586 void anchor() override; 587 588 public: 589 // Offloading bundling doesn't change the type of output. 590 OffloadBundlingJobAction(ActionList &Inputs); 591 592 static bool classof(const Action *A) { 593 return A->getKind() == OffloadBundlingJobClass; 594 } 595 }; 596 597 class OffloadUnbundlingJobAction final : public JobAction { 598 void anchor() override; 599 600 public: 601 /// Type that provides information about the actions that depend on this 602 /// unbundling action. 603 struct DependentActionInfo final { 604 /// The tool chain of the dependent action. 605 const ToolChain *DependentToolChain = nullptr; 606 607 /// The bound architecture of the dependent action. 608 StringRef DependentBoundArch; 609 610 /// The offload kind of the dependent action. 611 const OffloadKind DependentOffloadKind = OFK_None; 612 613 DependentActionInfo(const ToolChain *DependentToolChain, 614 StringRef DependentBoundArch, 615 const OffloadKind DependentOffloadKind) 616 : DependentToolChain(DependentToolChain), 617 DependentBoundArch(DependentBoundArch), 618 DependentOffloadKind(DependentOffloadKind) {} 619 }; 620 621 private: 622 /// Container that keeps information about each dependence of this unbundling 623 /// action. 624 SmallVector<DependentActionInfo, 6> DependentActionInfoArray; 625 626 public: 627 // Offloading unbundling doesn't change the type of output. 628 OffloadUnbundlingJobAction(Action *Input); 629 630 /// Register information about a dependent action. 631 void registerDependentActionInfo(const ToolChain *TC, StringRef BoundArch, 632 OffloadKind Kind) { 633 DependentActionInfoArray.push_back({TC, BoundArch, Kind}); 634 } 635 636 /// Return the information about all depending actions. 637 ArrayRef<DependentActionInfo> getDependentActionsInfo() const { 638 return DependentActionInfoArray; 639 } 640 641 static bool classof(const Action *A) { 642 return A->getKind() == OffloadUnbundlingJobClass; 643 } 644 }; 645 646 class OffloadPackagerJobAction : public JobAction { 647 void anchor() override; 648 649 public: 650 OffloadPackagerJobAction(ActionList &Inputs, types::ID Type); 651 652 static bool classof(const Action *A) { 653 return A->getKind() == OffloadPackagerJobClass; 654 } 655 }; 656 657 class LinkerWrapperJobAction : public JobAction { 658 void anchor() override; 659 660 public: 661 LinkerWrapperJobAction(ActionList &Inputs, types::ID Type); 662 663 static bool classof(const Action *A) { 664 return A->getKind() == LinkerWrapperJobClass; 665 } 666 }; 667 668 class StaticLibJobAction : public JobAction { 669 void anchor() override; 670 671 public: 672 StaticLibJobAction(ActionList &Inputs, types::ID Type); 673 674 static bool classof(const Action *A) { 675 return A->getKind() == StaticLibJobClass; 676 } 677 }; 678 679 class BinaryAnalyzeJobAction : public JobAction { 680 void anchor() override; 681 682 public: 683 BinaryAnalyzeJobAction(Action *Input, types::ID Type); 684 685 static bool classof(const Action *A) { 686 return A->getKind() == BinaryAnalyzeJobClass; 687 } 688 }; 689 690 } // namespace driver 691 } // namespace clang 692 693 #endif // LLVM_CLANG_DRIVER_ACTION_H 694