jit_gen.cc 2.6 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/fluid/operators/math/jit_gen.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include "paddle/fluid/platform/cpu_info.h"

DEFINE_bool(dump_jitcode, false, "Whether to dump the jitcode to file");

namespace paddle {
namespace operators {
namespace math {
namespace jitkernel {
namespace gen {

constexpr Xbyak::Operand::Code g_abi_regs[] = {
    Xbyak::Operand::RBX, Xbyak::Operand::RBP, Xbyak::Operand::R12,
    Xbyak::Operand::R13, Xbyak::Operand::R14, Xbyak::Operand::R15};

constexpr int num_g_abi_regs = sizeof(g_abi_regs) / sizeof(g_abi_regs[0]);

void JitCode::preCode() {
  for (int i = 0; i < num_g_abi_regs; ++i) {
    push(Xbyak::Reg64(g_abi_regs[i]));
  }
T
tensor-tang 已提交
39
  if (platform::MayIUse(platform::avx512f)) {
T
tensor-tang 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    mov(reg_EVEX_max_8b_offt, 2 * EVEX_max_8b_offt);
  }
}

void JitCode::postCode() {
  for (int i = 0; i < num_g_abi_regs; ++i) {
    pop(Xbyak::Reg64(g_abi_regs[num_g_abi_regs - 1 - i]));
  }
  ret();
}

void JitCode::dumpCode(const Xbyak::uint8 *code) const {
  if (code) {
    static int counter = 0;
    std::ostringstream filename;
    filename << "paddle_jitcode_" << name() << "." << counter << ".bin";
    counter++;
    std::ofstream fout(filename.str(), std::ios::out);
    if (fout.is_open()) {
      fout.write(reinterpret_cast<const char *>(code), getSize());
      fout.close();
    }
  }
}

Xbyak::Address JitCode::EVEX_compress_addr(Xbyak::Reg64 base, int offt,
                                           bool bcast) {
  int scale = 0;
  if (EVEX_max_8b_offt <= offt && offt < 3 * EVEX_max_8b_offt) {
    offt = offt - 2 * EVEX_max_8b_offt;
    scale = 1;
  } else if (3 * EVEX_max_8b_offt <= offt && offt < 5 * EVEX_max_8b_offt) {
    offt = offt - 4 * EVEX_max_8b_offt;
    scale = 2;
  }
  auto re = Xbyak::RegExp() + base + offt;
  if (scale) {
    re = re + reg_EVEX_max_8b_offt * scale;
  }
  if (bcast) {
    return zword_b[re];
  } else {
    return zword[re];
  }
}

}  // namespace gen
}  // namespace jitkernel
}  // namespace math
}  // namespace operators
}  // namespace paddle