File size: 7,903 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
{
  "name": "baseline-simd-loop-sve_loop_038",
  "definition": "loop_038",
  "dataset": "simd-loop",
  "author": "baseline-simd-loop-sve",
  "spec": {
    "language": "cpp",
    "target_hardware": [
      "aarch64"
    ],
    "entry_point": "kernel.cpp::inner_loop_038",
    "dependencies": [],
    "isa_features": [
      "sve"
    ],
    "compile_flags": [
      "-O3",
      "-std=c++14",
      "-march=armv8.2-a+sve",
      "-Wno-c++11-narrowing"
    ],
    "link_flags": []
  },
  "sources": [
    {
      "path": "loop_038.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_038_data {\n    _Float16 *a;\n    _Float16 *b;\n    _Float16 *c;\n    int dim;\n};\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\nint kernelshift_entry_loop_038(void *a, void *b, int64_t dim, void *res_out);\n#ifdef __cplusplus\n}\n#endif\n"
    },
    {
      "path": "loop_038.cpp",
      "content": "// Auto-generated by scripts/gen_simd_loop_harness.py \u2014 do not hand-edit.\n// Expert-baseline binding: pads edge-size workloads to the sizes Arm's kernel\n// supports (see _SVE_PAD_BINDINGS in the generator). Plain call otherwise.\n#include \"loop_038.h\"\n#include <stdint.h>\n#include <stdlib.h>\n#include <string.h>\n// Vector-length units for the padding rules below. Arm's kernel is built for\n// 128-bit SVE (Graviton3/4), so a build without SVE (the neon arm of an ISA\n// ablation lifts this binding too) pads to the same sizes with constants.\n#if defined(__ARM_FEATURE_SVE)\n#include <arm_sve.h>\n#define _VLW() ((uint64_t)svcntw())\n#define _VLH() ((uint64_t)svcnth())\n#else\n#define _VLW() ((uint64_t)4)\n#define _VLH() ((uint64_t)8)\n#endif\n\nextern \"C\" void inner_loop_038(struct loop_038_data *data);\n\nstatic inline uint64_t _round_up(uint64_t v, uint64_t m) { return (v + m - 1) / m * m; }\nstatic inline void *_zeroed(uint64_t bytes) { void *p = calloc(bytes ? bytes : 1, 1); if (!p) abort(); return p; }\n\nextern \"C\" int kernelshift_entry_loop_038(void *a, void *b, int64_t dim_in, void *res_out) {\n    const uint64_t dim = (uint64_t)dim_in, vlh = _VLH(), two = 2 * vlh;\n    struct loop_038_data _kd;\n    _Float16 *c = static_cast<_Float16 *>(res_out);\n    if (dim < 2) { memset(c, 0, dim * dim * sizeof(_Float16)); return 0; }\n    const uint64_t r = (dim - 1) % two;\n    if (r == 0 || r > vlh) {\n        _kd.a = static_cast<_Float16 *>(a); _kd.b = static_cast<_Float16 *>(b); _kd.c = c; _kd.dim = (int)dim;\n        inner_loop_038(&_kd);\n        return 0;\n    }\n    const uint64_t dp = _round_up(dim - 1, two) + 1;\n    _Float16 *pa = (_Float16 *)_zeroed(dp * dp * sizeof(_Float16)), *pb = (_Float16 *)_zeroed(dp * dp * sizeof(_Float16)), *pc = (_Float16 *)_zeroed(dp * dp * sizeof(_Float16));\n    for (uint64_t row = 0; row < dim; ++row) {\n        memcpy(pa + row * dp, static_cast<_Float16 *>(a) + row * dim, dim * sizeof(_Float16));\n        memcpy(pb + row * dp, static_cast<_Float16 *>(b) + row * dim, dim * sizeof(_Float16));\n    }\n    _kd.a = pa; _kd.b = pb; _kd.c = pc; _kd.dim = (int)dp;\n    inner_loop_038(&_kd);\n    memset(c, 0, dim * dim * sizeof(_Float16));\n    for (uint64_t row = 0; row + 1 < dim; ++row)\n        memcpy(c + row * dim, pc + row * dp, (dim - 1) * sizeof(_Float16));\n    free(pa); free(pb); free(pc);\n    return 0;\n}\n"
    },
    {
      "path": "kernel.cpp",
      "content": "#include \"loop_038.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#define SC_SVE_ATTR\n#define SC_SVE_LOOP_ATTR\n#define NS_SVE_LOOP_ATTR\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) {\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_sve_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\nextern \"C\" void inner_loop_038(struct loop_038_data * data)\n\n{\n  float16_t *a = (float16_t *)data->a;\n  float16_t *b = (float16_t *)data->b;\n  float16_t *c = (float16_t *)data->c;\n  int64_t dim = data->dim;\n  svfloat16_t k = svdup_f16(0.25f);\n  for (int row = 0; row < dim - 1; row++) {\n    int64_t i0 = 0;\n    int64_t i1 = svcnth();\n    float16_t *s0 = a + (row * dim);\n    float16_t *s1 = a + (row * dim) + 1;\n    float16_t *s2 = a + ((row + 1) * dim);\n    float16_t *s3 = a + ((row + 1) * dim) + 1;\n    float16_t *ac = b + (row * dim);\n    float16_t *ad = c + (row * dim);\n    asm volatile(\n        \"       ptrue   p0.h                                    \\n\"\n        \"       b       2f                                      \\n\"\n        \"1:     ld1h    {z10.h}, p0/z, [%[ac], %[i0], lsl #1]   \\n\"\n        \"       ld1h    {z11.h}, p1/z, [%[ac], %[i1], lsl #1]   \\n\"\n        \"       ld1h    {z0.h},  p0/z, [%[s0], %[i0], lsl #1]   \\n\"\n        \"       ld1h    {z1.h},  p1/z, [%[s0], %[i1], lsl #1]   \\n\"\n        \"       ld1h    {z2.h},  p0/z, [%[s1], %[i0], lsl #1]   \\n\"\n        \"       ld1h    {z3.h},  p1/z, [%[s1], %[i1], lsl #1]   \\n\"\n        \"       ld1h    {z4.h},  p0/z, [%[s2], %[i0], lsl #1]   \\n\"\n        \"       ld1h    {z5.h},  p1/z, [%[s2], %[i1], lsl #1]   \\n\"\n        \"       ld1h    {z6.h},  p0/z, [%[s3], %[i0], lsl #1]   \\n\"\n        \"       ld1h    {z7.h},  p1/z, [%[s3], %[i1], lsl #1]   \\n\"\n        \"       fmla    z10.h, p0/m, z0.h, %[k].h               \\n\"\n        \"       fmla    z11.h, p0/m, z1.h, %[k].h               \\n\"\n        \"       fmla    z10.h, p0/m, z2.h, %[k].h               \\n\"\n        \"       fmla    z11.h, p0/m, z3.h, %[k].h               \\n\"\n        \"       fmla    z10.h, p0/m, z4.h, %[k].h               \\n\"\n        \"       fmla    z11.h, p0/m, z5.h, %[k].h               \\n\"\n        \"       fmla    z10.h, p0/m, z6.h, %[k].h               \\n\"\n        \"       fmla    z11.h, p0/m, z7.h, %[k].h               \\n\"\n        \"       st1h    {z10.h}, p0, [%[ad], %[i0], lsl #1]     \\n\"\n        \"       st1h    {z11.h}, p1, [%[ad], %[i1], lsl #1]     \\n\"\n        \"       incb    %[i0], all                              \\n\"\n        \"       incb    %[i1], all                              \\n\"\n        \"2:     whilelo p1.h, %[i1], %[dim]                     \\n\"\n        \"       b.first    1b                                   \\n\"\n        : [i0] \"+&r\"(i0), [i1] \"+&r\"(i1)\n        : [s0] \"r\"(s0), [s1] \"r\"(s1), [s2] \"r\"(s2), [s3] \"r\"(s3), [ac] \"r\"(ac),\n          [ad] \"r\"(ad), [dim] \"r\"(dim - 1), [k] \"w\"(k)\n        : \"v0\", \"v1\", \"v2\", \"v3\", \"v4\", \"v5\", \"v6\", \"v7\", \"v10\", \"v11\", \"cc\",\n          \"p0\", \"p1\", \"cc\", \"memory\");\n  }\n}\n#define DIM 128\n"
    }
  ],
  "description": "Arm hand-written SVE kernel for loop_038 (sve tier expert ceiling)."
}