Commit 4a021333 authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Add ability to retrieve the function args context in PyXASM



Unlike C++, in Python, we need to know which variables have been declared in the function scope (including func. args) so that we can add *auto * when seeing assigment expressions.

Hence, the syntax handler will cache the map of kernel signatures. Just reuse the appendKernel util to cache kernel signature along the kernel name.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 636ced52
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -80,7 +80,7 @@ void QCORSyntaxHandler::GetReplacement(

  // Get Tokens as a string, rewrite code
  // with XACC api calls
  qcor::append_kernel(kernel_name);
  qcor::append_kernel(kernel_name, program_arg_types, program_parameters);

  auto new_src = qcor::run_token_collector(PP, Toks, bufferNames);

+8 −1
Original line number Diff line number Diff line
@@ -100,7 +100,14 @@ void PyXasmTokenCollector::collect(clang::Preprocessor &PP,

  int previous_col = lines[0].second;
  int line_counter = 0;
  std::vector<std::string> local_vars;
  const std::string kernel_name = ::quantum::kernels_in_translation_unit.back();
  const auto &[arg_types, arg_names] =
      ::quantum::kernel_signatures_in_translation_unit[kernel_name];
  
  // Add all the kernel args to the list of *known* arguments.
  // i.e. when we see an assignment expression where this arg. is the LHS,
  // we don't add *auto * to the codegen.
  std::vector<std::string> local_vars = arg_names;
  // Tracking the Python scopes by the indent of code blocks
  std::stack<int> scope_block_indent;
  for (const auto &line : lines) {
+8 −6
Original line number Diff line number Diff line
@@ -201,6 +201,7 @@ public:
          result.first = ss.str();
        }
        else {
          if (!context->trailer().empty()) {
            // A classical call-like expression: i.e. not a kernel call:
            // Just output it *as-is* to the C++ stream.
            // We can hook more sophisticated code-gen here if required.
@@ -210,6 +211,7 @@ public:
          }
        }
      }
    }
    return 0;
  }

+5 −1
Original line number Diff line number Diff line
@@ -16,8 +16,12 @@
#include "qcor_config.hpp"

namespace qcor {
void append_kernel(const std::string name) {
void append_kernel(const std::string name,
                   const std::vector<std::string> &program_arg_types,
                   const std::vector<std::string> &program_parameters) {
  ::quantum::kernels_in_translation_unit.push_back(name);
  ::quantum::kernel_signatures_in_translation_unit[name] =
      std::make_pair(program_arg_types, program_parameters);
}

void set_verbose(bool verbose) { xacc::set_verbose(verbose); }
+3 −1
Original line number Diff line number Diff line
@@ -6,7 +6,9 @@
#include <sstream>

namespace qcor {
void append_kernel(const std::string name);
void append_kernel(const std::string name,
                   const std::vector<std::string> &program_arg_types,
                   const std::vector<std::string> &program_parameters);
std::string run_token_collector(clang::Preprocessor &PP,
                                clang::CachedTokens &Toks,
                                std::vector<std::string> bufferNames);
Loading