| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 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 | } |
| 28 | } |
| 29 | |
| 30 | namespace clang { |
| 31 | namespace driver { |
| 32 | |
| 33 | class ToolChain; |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 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 | , |
| 62 | AnalyzeJobClass, |
| 63 | MigrateJobClass, |
| 64 | CompileJobClass, |
| 65 | BackendJobClass, |
| 66 | AssembleJobClass, |
| 67 | LinkJobClass, |
| 68 | LipoJobClass, |
| 69 | DsymutilJobClass, |
| 70 | VerifyDebugInfoJobClass, |
| 71 | VerifyPCHJobClass, |
| 72 | OffloadBundlingJobClass, |
| 73 | OffloadUnbundlingJobClass, |
| 74 | |
| 75 | JobClassFirst = PreprocessJobClass, |
| 76 | JobClassLast = OffloadUnbundlingJobClass |
| 77 | }; |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | enum OffloadKind { |
| 83 | OFK_None = 0x00, |
| 84 | |
| 85 | |
| 86 | OFK_Host = 0x01, |
| 87 | |
| 88 | |
| 89 | OFK_Cuda = 0x02, |
| 90 | OFK_OpenMP = 0x04, |
| 91 | OFK_HIP = 0x08, |
| 92 | }; |
| 93 | |
| 94 | static const char *getClassName(ActionClass AC); |
| 95 | |
| 96 | private: |
| 97 | ActionClass Kind; |
| 98 | |
| 99 | |
| 100 | types::ID Type; |
| 101 | |
| 102 | ActionList Inputs; |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | bool CanBeCollapsedWithNextDependentAction = true; |
| 109 | |
| 110 | protected: |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | unsigned ActiveOffloadKindMask = 0u; |
| 119 | |
| 120 | |
| 121 | OffloadKind OffloadingDeviceKind = OFK_None; |
| 122 | |
| 123 | |
| 124 | const char *OffloadingArch = nullptr; |
| 125 | |
| 126 | Action(ActionClass Kind, types::ID Type) : Action(Kind, ActionList(), Type) {} |
| 127 | Action(ActionClass Kind, Action *Input, types::ID Type) |
| 128 | : Action(Kind, ActionList({Input}), Type) {} |
| 129 | Action(ActionClass Kind, Action *Input) |
| 130 | : Action(Kind, ActionList({Input}), Input->getType()) {} |
| 131 | Action(ActionClass Kind, const ActionList &Inputs, types::ID Type) |
| 132 | : Kind(Kind), Type(Type), Inputs(Inputs) {} |
| 133 | |
| 134 | public: |
| 135 | virtual ~Action(); |
| 136 | |
| 137 | const char *getClassName() const { return Action::getClassName(getKind()); } |
| 138 | |
| 139 | ActionClass getKind() const { return Kind; } |
| 140 | types::ID getType() const { return Type; } |
| 141 | |
| 142 | ActionList &getInputs() { return Inputs; } |
| 143 | const ActionList &getInputs() const { return Inputs; } |
| 144 | |
| 145 | size_type size() const { return Inputs.size(); } |
| 146 | |
| 147 | input_iterator input_begin() { return Inputs.begin(); } |
| 148 | input_iterator input_end() { return Inputs.end(); } |
| 149 | input_range inputs() { return input_range(input_begin(), input_end()); } |
| 150 | input_const_iterator input_begin() const { return Inputs.begin(); } |
| 151 | input_const_iterator input_end() const { return Inputs.end(); } |
| 152 | input_const_range inputs() const { |
| 153 | return input_const_range(input_begin(), input_end()); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | void setCannotBeCollapsedWithNextDependentAction() { |
| 158 | CanBeCollapsedWithNextDependentAction = false; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | bool isCollapsingWithNextDependentActionLegal() const { |
| 163 | return CanBeCollapsedWithNextDependentAction; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | std::string getOffloadingKindPrefix() const; |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | static std::string |
| 173 | GetOffloadingFileNamePrefix(OffloadKind Kind, |
| 174 | StringRef NormalizedTriple, |
| 175 | bool CreatePrefixForHost = false); |
| 176 | |
| 177 | |
| 178 | static StringRef GetOffloadKindName(OffloadKind Kind); |
| 179 | |
| 180 | |
| 181 | |
| 182 | void propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch); |
| 183 | |
| 184 | |
| 185 | |
| 186 | void propagateHostOffloadInfo(unsigned OKinds, const char *OArch); |
| 187 | |
| 188 | |
| 189 | |
| 190 | void propagateOffloadInfo(const Action *A); |
| 191 | |
| 192 | unsigned getOffloadingHostActiveKinds() const { |
| 193 | return ActiveOffloadKindMask; |
| 194 | } |
| 195 | |
| 196 | OffloadKind getOffloadingDeviceKind() const { return OffloadingDeviceKind; } |
| 197 | const char *getOffloadingArch() const { return OffloadingArch; } |
| 198 | |
| 199 | |
| 200 | |
| 201 | bool isHostOffloading(OffloadKind OKind) const { |
| 202 | return ActiveOffloadKindMask & OKind; |
| 203 | } |
| 204 | bool isDeviceOffloading(OffloadKind OKind) const { |
| 205 | return OffloadingDeviceKind == OKind; |
| 206 | } |
| 207 | bool isOffloading(OffloadKind OKind) const { |
| 208 | return isHostOffloading(OKind) || isDeviceOffloading(OKind); |
| 209 | } |
| 210 | }; |
| 211 | |
| 212 | class InputAction : public Action { |
| 213 | const llvm::opt::Arg &Input; |
| 214 | |
| 215 | virtual void anchor(); |
| 216 | |
| 217 | public: |
| 218 | InputAction(const llvm::opt::Arg &Input, types::ID Type); |
| 219 | |
| 220 | const llvm::opt::Arg &getInputArg() const { return Input; } |
| 221 | |
| 222 | static bool classof(const Action *A) { |
| 223 | return A->getKind() == InputClass; |
| 224 | } |
| 225 | }; |
| 226 | |
| 227 | class BindArchAction : public Action { |
| 228 | virtual void anchor(); |
| 229 | |
| 230 | |
| 231 | |
| 232 | StringRef ArchName; |
| 233 | |
| 234 | public: |
| 235 | BindArchAction(Action *Input, StringRef ArchName); |
| 236 | |
| 237 | StringRef getArchName() const { return ArchName; } |
| 238 | |
| 239 | static bool classof(const Action *A) { |
| 240 | return A->getKind() == BindArchClass; |
| 241 | } |
| 242 | }; |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | class OffloadAction final : public Action { |
| 248 | virtual void anchor(); |
| 249 | |
| 250 | public: |
| 251 | |
| 252 | |
| 253 | class DeviceDependences final { |
| 254 | public: |
| 255 | using ToolChainList = SmallVector<const ToolChain *, 3>; |
| 256 | using BoundArchList = SmallVector<const char *, 3>; |
| 257 | using OffloadKindList = SmallVector<OffloadKind, 3>; |
| 258 | |
| 259 | private: |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | ActionList DeviceActions; |
| 267 | |
| 268 | |
| 269 | ToolChainList DeviceToolChains; |
| 270 | |
| 271 | |
| 272 | BoundArchList DeviceBoundArchs; |
| 273 | |
| 274 | |
| 275 | OffloadKindList DeviceOffloadKinds; |
| 276 | |
| 277 | public: |
| 278 | |
| 279 | |
| 280 | void add(Action &A, const ToolChain &TC, const char *BoundArch, |
| 281 | OffloadKind OKind); |
| 282 | |
| 283 | |
| 284 | const ActionList &getActions() const { return DeviceActions; } |
| 285 | const ToolChainList &getToolChains() const { return DeviceToolChains; } |
| 286 | const BoundArchList &getBoundArchs() const { return DeviceBoundArchs; } |
| 287 | const OffloadKindList &getOffloadKinds() const { |
| 288 | return DeviceOffloadKinds; |
| 289 | } |
| 290 | }; |
| 291 | |
| 292 | |
| 293 | |
| 294 | class HostDependence final { |
| 295 | |
| 296 | Action &HostAction; |
| 297 | |
| 298 | |
| 299 | const ToolChain &HostToolChain; |
| 300 | |
| 301 | |
| 302 | const char *HostBoundArch = nullptr; |
| 303 | |
| 304 | |
| 305 | unsigned HostOffloadKinds = 0u; |
| 306 | |
| 307 | public: |
| 308 | HostDependence(Action &A, const ToolChain &TC, const char *BoundArch, |
| 309 | const unsigned OffloadKinds) |
| 310 | : HostAction(A), HostToolChain(TC), HostBoundArch(BoundArch), |
| 311 | HostOffloadKinds(OffloadKinds) {} |
| 312 | |
| 313 | |
| 314 | |
| 315 | HostDependence(Action &A, const ToolChain &TC, const char *BoundArch, |
| 316 | const DeviceDependences &DDeps); |
| 317 | Action *getAction() const { return &HostAction; } |
| 318 | const ToolChain *getToolChain() const { return &HostToolChain; } |
| 319 | const char *getBoundArch() const { return HostBoundArch; } |
| 320 | unsigned getOffloadKinds() const { return HostOffloadKinds; } |
| 321 | }; |
| 322 | |
| 323 | using OffloadActionWorkTy = |
| 324 | llvm::function_ref<void(Action *, const ToolChain *, const char *)>; |
| 325 | |
| 326 | private: |
| 327 | |
| 328 | const ToolChain *HostTC = nullptr; |
| 329 | |
| 330 | |
| 331 | DeviceDependences::ToolChainList DevToolChains; |
| 332 | |
| 333 | public: |
| 334 | OffloadAction(const HostDependence &HDep); |
| 335 | OffloadAction(const DeviceDependences &DDeps, types::ID Ty); |
| 336 | OffloadAction(const HostDependence &HDep, const DeviceDependences &DDeps); |
| 337 | |
| 338 | |
| 339 | void doOnHostDependence(const OffloadActionWorkTy &Work) const; |
| 340 | |
| 341 | |
| 342 | void doOnEachDeviceDependence(const OffloadActionWorkTy &Work) const; |
| 343 | |
| 344 | |
| 345 | void doOnEachDependence(const OffloadActionWorkTy &Work) const; |
| 346 | |
| 347 | |
| 348 | |
| 349 | void doOnEachDependence(bool IsHostDependence, |
| 350 | const OffloadActionWorkTy &Work) const; |
| 351 | |
| 352 | |
| 353 | bool hasHostDependence() const; |
| 354 | |
| 355 | |
| 356 | |
| 357 | Action *getHostDependence() const; |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | bool hasSingleDeviceDependence(bool DoNotConsiderHostActions = false) const; |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | Action * |
| 368 | getSingleDeviceDependence(bool DoNotConsiderHostActions = false) const; |
| 369 | |
| 370 | static bool classof(const Action *A) { return A->getKind() == OffloadClass; } |
| 371 | }; |
| 372 | |
| 373 | class JobAction : public Action { |
| 374 | virtual void anchor(); |
| 375 | |
| 376 | protected: |
| 377 | JobAction(ActionClass Kind, Action *Input, types::ID Type); |
| 378 | JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type); |
| 379 | |
| 380 | public: |
| 381 | static bool classof(const Action *A) { |
| 382 | return (A->getKind() >= JobClassFirst && |
| 383 | A->getKind() <= JobClassLast); |
| 384 | } |
| 385 | }; |
| 386 | |
| 387 | class PreprocessJobAction : public JobAction { |
| 388 | void anchor() override; |
| 389 | |
| 390 | public: |
| 391 | PreprocessJobAction(Action *Input, types::ID OutputType); |
| 392 | |
| 393 | static bool classof(const Action *A) { |
| 394 | return A->getKind() == PreprocessJobClass; |
| 395 | } |
| 396 | }; |
| 397 | |
| 398 | class PrecompileJobAction : public JobAction { |
| 399 | void anchor() override; |
| 400 | |
| 401 | protected: |
| 402 | PrecompileJobAction(ActionClass Kind, Action *Input, types::ID OutputType); |
| 403 | |
| 404 | public: |
| 405 | PrecompileJobAction(Action *Input, types::ID OutputType); |
| 406 | |
| 407 | static bool classof(const Action *A) { |
| 408 | return A->getKind() == PrecompileJobClass || |
| 409 | A->getKind() == HeaderModulePrecompileJobClass; |
| 410 | } |
| 411 | }; |
| 412 | |
| 413 | class : public PrecompileJobAction { |
| 414 | void () override; |
| 415 | |
| 416 | const char *; |
| 417 | |
| 418 | public: |
| 419 | HeaderModulePrecompileJobAction(Action *Input, types::ID OutputType, |
| 420 | const char *ModuleName); |
| 421 | |
| 422 | static bool (const Action *A) { |
| 423 | return A->getKind() == HeaderModulePrecompileJobClass; |
| 424 | } |
| 425 | |
| 426 | void (Action *Input) { |
| 427 | getInputs().push_back(Input); |
| 428 | } |
| 429 | |
| 430 | const char *() const { return ModuleName; } |
| 431 | }; |
| 432 | |
| 433 | class AnalyzeJobAction : public JobAction { |
| 434 | void anchor() override; |
| 435 | |
| 436 | public: |
| 437 | AnalyzeJobAction(Action *Input, types::ID OutputType); |
| 438 | |
| 439 | static bool classof(const Action *A) { |
| 440 | return A->getKind() == AnalyzeJobClass; |
| 441 | } |
| 442 | }; |
| 443 | |
| 444 | class MigrateJobAction : public JobAction { |
| 445 | void anchor() override; |
| 446 | |
| 447 | public: |
| 448 | MigrateJobAction(Action *Input, types::ID OutputType); |
| 449 | |
| 450 | static bool classof(const Action *A) { |
| 451 | return A->getKind() == MigrateJobClass; |
| 452 | } |
| 453 | }; |
| 454 | |
| 455 | class CompileJobAction : public JobAction { |
| 456 | void anchor() override; |
| 457 | |
| 458 | public: |
| 459 | CompileJobAction(Action *Input, types::ID OutputType); |
| 460 | |
| 461 | static bool classof(const Action *A) { |
| 462 | return A->getKind() == CompileJobClass; |
| 463 | } |
| 464 | }; |
| 465 | |
| 466 | class BackendJobAction : public JobAction { |
| 467 | void anchor() override; |
| 468 | |
| 469 | public: |
| 470 | BackendJobAction(Action *Input, types::ID OutputType); |
| 471 | |
| 472 | static bool classof(const Action *A) { |
| 473 | return A->getKind() == BackendJobClass; |
| 474 | } |
| 475 | }; |
| 476 | |
| 477 | class AssembleJobAction : public JobAction { |
| 478 | void anchor() override; |
| 479 | |
| 480 | public: |
| 481 | AssembleJobAction(Action *Input, types::ID OutputType); |
| 482 | |
| 483 | static bool classof(const Action *A) { |
| 484 | return A->getKind() == AssembleJobClass; |
| 485 | } |
| 486 | }; |
| 487 | |
| 488 | class LinkJobAction : public JobAction { |
| 489 | void anchor() override; |
| 490 | |
| 491 | public: |
| 492 | LinkJobAction(ActionList &Inputs, types::ID Type); |
| 493 | |
| 494 | static bool classof(const Action *A) { |
| 495 | return A->getKind() == LinkJobClass; |
| 496 | } |
| 497 | }; |
| 498 | |
| 499 | class LipoJobAction : public JobAction { |
| 500 | void anchor() override; |
| 501 | |
| 502 | public: |
| 503 | LipoJobAction(ActionList &Inputs, types::ID Type); |
| 504 | |
| 505 | static bool classof(const Action *A) { |
| 506 | return A->getKind() == LipoJobClass; |
| 507 | } |
| 508 | }; |
| 509 | |
| 510 | class DsymutilJobAction : public JobAction { |
| 511 | void anchor() override; |
| 512 | |
| 513 | public: |
| 514 | DsymutilJobAction(ActionList &Inputs, types::ID Type); |
| 515 | |
| 516 | static bool classof(const Action *A) { |
| 517 | return A->getKind() == DsymutilJobClass; |
| 518 | } |
| 519 | }; |
| 520 | |
| 521 | class VerifyJobAction : public JobAction { |
| 522 | void anchor() override; |
| 523 | |
| 524 | public: |
| 525 | VerifyJobAction(ActionClass Kind, Action *Input, types::ID Type); |
| 526 | |
| 527 | static bool classof(const Action *A) { |
| 528 | return A->getKind() == VerifyDebugInfoJobClass || |
| 529 | A->getKind() == VerifyPCHJobClass; |
| 530 | } |
| 531 | }; |
| 532 | |
| 533 | class VerifyDebugInfoJobAction : public VerifyJobAction { |
| 534 | void anchor() override; |
| 535 | |
| 536 | public: |
| 537 | VerifyDebugInfoJobAction(Action *Input, types::ID Type); |
| 538 | |
| 539 | static bool classof(const Action *A) { |
| 540 | return A->getKind() == VerifyDebugInfoJobClass; |
| 541 | } |
| 542 | }; |
| 543 | |
| 544 | class VerifyPCHJobAction : public VerifyJobAction { |
| 545 | void anchor() override; |
| 546 | |
| 547 | public: |
| 548 | VerifyPCHJobAction(Action *Input, types::ID Type); |
| 549 | |
| 550 | static bool classof(const Action *A) { |
| 551 | return A->getKind() == VerifyPCHJobClass; |
| 552 | } |
| 553 | }; |
| 554 | |
| 555 | class OffloadBundlingJobAction : public JobAction { |
| 556 | void anchor() override; |
| 557 | |
| 558 | public: |
| 559 | |
| 560 | OffloadBundlingJobAction(ActionList &Inputs); |
| 561 | |
| 562 | static bool classof(const Action *A) { |
| 563 | return A->getKind() == OffloadBundlingJobClass; |
| 564 | } |
| 565 | }; |
| 566 | |
| 567 | class OffloadUnbundlingJobAction final : public JobAction { |
| 568 | void anchor() override; |
| 569 | |
| 570 | public: |
| 571 | |
| 572 | |
| 573 | struct DependentActionInfo final { |
| 574 | |
| 575 | const ToolChain *DependentToolChain = nullptr; |
| 576 | |
| 577 | |
| 578 | StringRef DependentBoundArch; |
| 579 | |
| 580 | |
| 581 | const OffloadKind DependentOffloadKind = OFK_None; |
| 582 | |
| 583 | DependentActionInfo(const ToolChain *DependentToolChain, |
| 584 | StringRef DependentBoundArch, |
| 585 | const OffloadKind DependentOffloadKind) |
| 586 | : DependentToolChain(DependentToolChain), |
| 587 | DependentBoundArch(DependentBoundArch), |
| 588 | DependentOffloadKind(DependentOffloadKind) {} |
| 589 | }; |
| 590 | |
| 591 | private: |
| 592 | |
| 593 | |
| 594 | SmallVector<DependentActionInfo, 6> DependentActionInfoArray; |
| 595 | |
| 596 | public: |
| 597 | |
| 598 | OffloadUnbundlingJobAction(Action *Input); |
| 599 | |
| 600 | |
| 601 | void registerDependentActionInfo(const ToolChain *TC, StringRef BoundArch, |
| 602 | OffloadKind Kind) { |
| 603 | DependentActionInfoArray.push_back({TC, BoundArch, Kind}); |
| 604 | } |
| 605 | |
| 606 | |
| 607 | ArrayRef<DependentActionInfo> getDependentActionsInfo() const { |
| 608 | return DependentActionInfoArray; |
| 609 | } |
| 610 | |
| 611 | static bool classof(const Action *A) { |
| 612 | return A->getKind() == OffloadUnbundlingJobClass; |
| 613 | } |
| 614 | }; |
| 615 | |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | #endif |
| 620 | |