File size: 8,586 Bytes
bbe836b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
{
  "name": "baseline-simd-loop-sme2_loop_223",
  "definition": "loop_223",
  "dataset": "simd-loop",
  "author": "baseline-simd-loop-sme2",
  "spec": {
    "language": "cpp",
    "target_hardware": [
      "aarch64"
    ],
    "entry_point": "kernel.cpp::inner_loop_223",
    "dependencies": [],
    "isa_features": [
      "sme2"
    ],
    "compile_flags": [
      "-O3",
      "-std=c++14",
      "-mcpu=apple-m4",
      "-Wno-c++11-narrowing"
    ],
    "link_flags": []
  },
  "sources": [
    {
      "path": "loop_223.h",
      "content": "// Auto-generated by scripts/gen_simd_loop_harness.py \u2014 do not hand-edit.\n#pragma once\n#include <stdint.h>\n\nstruct loop_223_data {\n    uint64_t m;\n    uint64_t n;\n    uint32_t *a;\n    uint32_t *at;\n};\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\nint kernelshift_entry_loop_223(void *a, int64_t m, int64_t n, void *res_out);\n#ifdef __cplusplus\n}\n#endif\n"
    },
    {
      "path": "loop_223.cpp",
      "content": "// Auto-generated by scripts/gen_simd_loop_harness.py \u2014 do not hand-edit.\n#include \"loop_223.h\"\n#include <stdint.h>\n\n#include <arm_sme.h>\n#define SC_SVE_LOOP_ATTR __arm_locally_streaming\n#define S_LOOP_ATTR __arm_locally_streaming __arm_new(\"za\", \"zt0\")\n#define NS_SVE_LOOP_ATTR\n#define SC_SVE_ATTR __arm_streaming_compatible\n#define S_SVE_ATTR __arm_streaming\n#define SC_SVE_ZT0_ATTR __arm_streaming_compatible __arm_inout(\"zt0\") __arm_preserves(\"za\")\n#define SME_ZA_ZT0_ATTR __arm_streaming __arm_inout(\"za\", \"zt0\")\n#define SME_ZA_ATTR __arm_streaming __arm_inout(\"za\") __arm_preserves(\"zt0\")\nstatic inline uint32_t get_sme_vl(void) {\n  uint64_t svl = 0; asm volatile(\"rdsvl %[svl], #8\" : [svl] \"+r\"(svl) : :);\n  return (uint32_t)svl;\n}\nextern \"C\" void inner_loop_223(struct loop_223_data *data) SME_ZA_ATTR;\nS_LOOP_ATTR static void __attribute__((noinline)) _kernelshift_call_223(struct loop_223_data *d) { inner_loop_223(d); }\n\nextern \"C\" int kernelshift_entry_loop_223(void *a, int64_t m, int64_t n, void *res_out) {\n    // Arm's documented shape rule for this kernel (see loops/loop_223.c);\n    // the workloads satisfy it, any other shape is refused.\n    if (!(m % 16 == 0)) return 1;\n    struct loop_223_data _kd;\n    _kd.a = static_cast<uint32_t *>(a);\n    _kd.at = static_cast<uint32_t *>(res_out);\n    _kd.m = static_cast<uint64_t>(m);\n    _kd.n = static_cast<uint64_t>(n);\n    _kernelshift_call_223(&_kd);\n    return 0;\n}\n"
    },
    {
      "path": "kernel.cpp",
      "content": "#include \"loop_223.h\"\n#include <stdint.h>\n#include <string.h>\n#include <stdlib.h>\n#include <stdio.h>\n#include <limits.h>\n#include <assert.h>\n#include <math.h>\n#include <arm_sve.h>\n#define restrict __restrict\n#include <arm_sme.h>\n#define SC_SVE_LOOP_ATTR __arm_locally_streaming\n#define S_LOOP_ATTR __arm_locally_streaming __arm_new(\"za\", \"zt0\")\n#define NS_SVE_LOOP_ATTR\n#define SC_SVE_ATTR __arm_streaming_compatible\n#define S_SVE_ATTR __arm_streaming\n#define SC_SVE_ZT0_ATTR __arm_streaming_compatible __arm_inout(\"zt0\") __arm_preserves(\"za\")\n#define SME_ZA_ZT0_ATTR __arm_streaming __arm_inout(\"za\", \"zt0\")\n#define SME_ZA_ATTR __arm_streaming __arm_inout(\"za\") __arm_preserves(\"zt0\")\nstatic inline uint32_t get_sme_vl(void) {\n  uint64_t svl = 0; asm volatile(\"rdsvl %[svl], #8\" : [svl] \"+r\"(svl) : :);\n  return (uint32_t)svl;\n}\n#define NOINLINE __attribute__((noinline))\n#define FOR_COND(P, S, I, N) svptest_first(svptrue_b##S(), P = svwhilelt_b##S(I, N))\n#define FOR_LOOP(T, I, M, N, P, S, W) for (T I = M; FOR_COND(P, S, I, N); I += svcnt##W())\n#define FOR_LOOP_8(T, I, M, N, P)  FOR_LOOP(T, I, M, N, P, 8, b)\n#define FOR_LOOP_16(T, I, M, N, P) FOR_LOOP(T, I, M, N, P, 16, h)\n#define FOR_LOOP_32(T, I, M, N, P) FOR_LOOP(T, I, M, N, P, 32, w)\n#define FOR_LOOP_64(T, I, M, N, P) FOR_LOOP(T, I, M, N, P, 64, d)\nstatic inline uint32_t get_sve_vl(void) SC_SVE_ATTR {\n  uint64_t vl = 0; asm volatile(\"incb %[vl], all, mul #8\" : [vl] \"+r\"(vl) : :);\n  return (uint32_t)vl;\n}\nstatic inline uint32_t get_vl(void) { return get_sme_vl(); }\n// common/helpers.h: 64-byte-aligned scratch. Only reached when the harness\n// hands a kernel a NULL scratch buffer (it never does \u2014 every scratch field is\n// bound), so a plain aligned malloc stands in for Arm's arena allocator.\nstatic inline void *alloc_64b(uint64_t size, const char *name) {\n  void *p = NULL; (void)name;\n  if (posix_memalign(&p, 64, size ? size : 64) != 0) abort();\n  return p;\n}\n#define ALLOC_64B(P, S, N) P = (__typeof__(P))alloc_64b((S) * sizeof((P)[0]), N)\n\n#define LOOP_ATTR SME_ZA_ATTR\n#define OUTER_LOOP_ATTR S_LOOP_ATTR\nextern \"C\" __arm_locally_streaming __arm_new(\"za\") void inner_loop_223(struct loop_223_data *data)\n{\n  uint64_t m = data->m;\n  uint64_t n = data->n;\n  uint32_t *a = data->a;\n  uint32_t *at = data->at;\n  uint32_t *a_ptr, *at_ptr;\n  uint64_t m_idx, n_idx, l_idx;\n  uint64_t svl_s = svcntw();\n  uint64_t blk_1 = svl_s * n;\n  uint64_t blk_2 = svl_s * m;\n  svbool_t p_all = svptrue_b32();\n  svcount_t pn_rows, pn_cols;\n  svbool_t p_rows_t, p_rows_b;\n  svbool_t p_cols_l, p_cols_h;\n  svuint32x2_t vec_a00, vec_a01, vec_a02, vec_a03;\n  svuint32x2_t vec_a10, vec_a11, vec_a12, vec_a13;\n  svuint32x4_t vec_at0, vec_at1, vec_at2, vec_at3;\n#define EXTR2(x,y,i) svget2(vec_a##x##y, i)\n#define LOAD(i,j,pn,p,idx,adr) { vec_a##i##j = svld1_x2(svpsel_lane_c32(pn, p, idx + j), &a_ptr[adr]); }\n#define LOAD_GROUP(i,pn,p,idx,adr) { LOAD(i, 0, pn, p, idx, adr); LOAD(i, 1, pn, p, idx, adr + n); LOAD(i, 2, pn, p, idx, adr + 2 * n); LOAD(i, 3, pn, p, idx, adr + 3 * n); };\n#define WRITE_ZA(i,j,l,idx) svwrite_hor_za32_vg4(i, (uint32_t)idx + 0, svcreate4(EXTR2(j, 0, l), EXTR2(j, 1, l), EXTR2(j, 2, l), EXTR2(j, 3, l)));\n#define STORE(i,j,l,pn,p,idx,adr) { svst1(svpsel_lane_c32(pn, p, idx + l), &at_ptr[adr], svcreate2(svget4(vec_at##i, l), svget4(vec_at##j, l))); }\n  for (m_idx = 0;; m_idx += svl_s * 2) {\n    pn_rows = svwhilelt_c32(m_idx, m, 2);\n    p_rows_b = svpext_lane_c32(pn_rows, 0);\n    p_rows_t = svpext_lane_c32(pn_rows, 1);\n    if (!svptest_first(p_all, p_rows_b)) break;\n    for (n_idx = 0;; n_idx += svl_s * 2) {\n      pn_cols = svwhilelt_c32(n_idx, n, 2);\n      p_cols_l = svpext_lane_c32(pn_cols, 0);\n      p_cols_h = svpext_lane_c32(pn_cols, 0);\n      if (!svptest_first(p_all, p_cols_l)) break;\n      a_ptr = &a[m_idx * n + n_idx];\n      for (l_idx = 0; l_idx < svl_s; l_idx += 4) {\n        LOAD_GROUP(0, pn_cols, p_rows_b, l_idx, 0);\n        LOAD_GROUP(1, pn_cols, p_rows_t, l_idx, blk_1);\n        WRITE_ZA(0, 0, 0, l_idx);\n        WRITE_ZA(1, 0, 1, l_idx);\n        WRITE_ZA(2, 1, 0, l_idx);\n        WRITE_ZA(3, 1, 1, l_idx);\n        a_ptr += n * 4;\n      }\n      at_ptr = &at[n_idx * m + m_idx];\n      for (l_idx = 0; l_idx < svl_s; l_idx += 4) {\n        vec_at0 = svread_ver_za32_u32_vg4(0, (uint32_t)l_idx + 0);\n        vec_at1 = svread_ver_za32_u32_vg4(2, (uint32_t)l_idx + 0);\n        vec_at2 = svread_ver_za32_u32_vg4(1, (uint32_t)l_idx + 0);\n        vec_at3 = svread_ver_za32_u32_vg4(3, (uint32_t)l_idx + 0);\n        STORE(0, 1, 0, pn_rows, p_cols_l, l_idx, 0 + 0);\n        STORE(0, 1, 1, pn_rows, p_cols_l, l_idx, 0 + m);\n        STORE(0, 1, 2, pn_rows, p_cols_l, l_idx, 0 + 2 * m);\n        STORE(0, 1, 3, pn_rows, p_cols_l, l_idx, 0 + 3 * m);\n        STORE(2, 3, 0, pn_rows, p_cols_h, l_idx, blk_2 + 0);\n        STORE(2, 3, 1, pn_rows, p_cols_h, l_idx, blk_2 + m);\n        STORE(2, 3, 2, pn_rows, p_cols_h, l_idx, blk_2 + 2 * m);\n        STORE(2, 3, 3, pn_rows, p_cols_h, l_idx, blk_2 + 3 * m);\n        at_ptr += m * 4;\n      }\n    }\n  }\n}\n#undef MAX_VL\n#define MAX_VL 2048\n#undef PROBLEM_SIZE_LIMIT_KIB\n#define PROBLEM_SIZE_LIMIT_KIB 256\n#define PROBLEM_SIZE_ACTUAL(m,n) ((m) * (n) * sizeof(uint32_t))\n"
    },
    {
      "path": "sme_abi_stub.cpp",
      "content": "// Minimal SME ABI support routines (see scripts/gen_simd_loop_harness.py).\nextern \"C\" {\n__attribute__((naked)) void __arm_tpidr2_save(void) { __asm__ volatile(\"ret\"); }\n__attribute__((naked)) void __arm_tpidr2_restore(void *) { __asm__ volatile(\"ret\"); }\n__attribute__((naked)) void __arm_za_disable(void) { __asm__ volatile(\"smstop za\\n ret\"); }\n__attribute__((naked)) void __arm_sme_state(void) { __asm__ volatile(\"mrs x0, svcr\\n mrs x1, tpidr2_el0\\n ret\"); }\n}\n"
    }
  ],
  "description": "Arm hand-written SVE kernel for loop_223 (sme2 tier expert ceiling). [M4: locally-streaming entry, -mcpu=apple-m4]"
}