SpirvFunction.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //===--- SpirvFunction.cpp - SPIR-V Function Implementation ------*- C++ -*-==//
  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. #include "clang/SPIRV/SpirvFunction.h"
  10. #include "BlockReadableOrder.h"
  11. #include "clang/SPIRV/SpirvBasicBlock.h"
  12. #include "clang/SPIRV/SpirvVisitor.h"
  13. namespace clang {
  14. namespace spirv {
  15. SpirvFunction::SpirvFunction(QualType returnType,
  16. llvm::ArrayRef<QualType> paramTypes,
  17. SourceLocation loc, llvm::StringRef name,
  18. bool isPrecise)
  19. : functionId(0), astReturnType(returnType),
  20. astParamTypes(paramTypes.begin(), paramTypes.end()), returnType(nullptr),
  21. fnType(nullptr), relaxedPrecision(false), precise(isPrecise),
  22. containsAlias(false), rvalue(false), functionLoc(loc),
  23. functionName(name) {}
  24. bool SpirvFunction::invokeVisitor(Visitor *visitor, bool reverseOrder) {
  25. if (!visitor->visit(this, Visitor::Phase::Init))
  26. return false;
  27. for (auto *param : parameters)
  28. visitor->visit(param);
  29. // Collect basic blocks in a human-readable order that satisfies SPIR-V
  30. // validation rules.
  31. std::vector<SpirvBasicBlock *> orderedBlocks;
  32. if (!basicBlocks.empty()) {
  33. BlockReadableOrderVisitor([&orderedBlocks](SpirvBasicBlock *block) {
  34. orderedBlocks.push_back(block);
  35. }).visit(basicBlocks.front());
  36. }
  37. SpirvBasicBlock *firstBB = orderedBlocks.empty() ? nullptr : orderedBlocks[0];
  38. if (reverseOrder)
  39. std::reverse(orderedBlocks.begin(), orderedBlocks.end());
  40. for (auto *bb : orderedBlocks) {
  41. // The first basic block of the function should first visit the function
  42. // variables.
  43. if (bb == firstBB) {
  44. if (!bb->invokeVisitor(visitor, variables, reverseOrder))
  45. return false;
  46. }
  47. // The rest of the basic blocks in the function do not need to visit
  48. // function variables.
  49. else {
  50. if (!bb->invokeVisitor(visitor, {}, reverseOrder))
  51. return false;
  52. }
  53. }
  54. if (!visitor->visit(this, Visitor::Phase::Done))
  55. return false;
  56. return true;
  57. }
  58. void SpirvFunction::addParameter(SpirvFunctionParameter *param) {
  59. assert(param && "cannot add null function parameter");
  60. parameters.push_back(param);
  61. }
  62. void SpirvFunction::addVariable(SpirvVariable *var) {
  63. assert(var && "cannot add null variable to function");
  64. variables.push_back(var);
  65. }
  66. void SpirvFunction::addBasicBlock(SpirvBasicBlock *bb) {
  67. assert(bb && "cannot add null basic block to function");
  68. basicBlocks.push_back(bb);
  69. }
  70. } // end namespace spirv
  71. } // end namespace clang