[all-commits] [llvm/llvm-project] 06eee7: [clang] Allow 'nomerge' attribute for function poi...

eddyz87 via All-commits all-commits at lists.llvm.org
Mon Jun 26 15:23:15 PDT 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 06eee734c1ea9de754c1e7e8c79c0897b9e01350
      https://github.com/llvm/llvm-project/commit/06eee734c1ea9de754c1e7e8c79c0897b9e01350
  Author: Eduard Zingerman <eddyz87 at gmail.com>
  Date:   2023-06-27 (Tue, 27 Jun 2023)

  Changed paths:
    M clang/include/clang/Basic/Attr.td
    M clang/include/clang/Basic/AttrDocs.td
    M clang/include/clang/Basic/DiagnosticSemaKinds.td
    M clang/lib/CodeGen/CGCall.cpp
    M clang/lib/Sema/SemaDeclAttr.cpp
    M clang/test/CodeGen/attr-nomerge.cpp
    M clang/test/Misc/pragma-attribute-supported-attributes-list.test
    M clang/test/Parser/stmt-attributes.c
    M clang/test/Parser/stmt-attributes.cpp
    M clang/test/Parser/stmt-attributes.m
    M clang/test/Sema/attr-nomerge-ast.cpp
    M clang/test/Sema/attr-nomerge.cpp

  Log Message:
  -----------
  [clang] Allow 'nomerge' attribute for function pointers

Allow specifying 'nomerge' attribute for function pointers,
e.g. like in the following C code:

    extern void (*foo)(void) __attribute__((nomerge));
    void bar(long i) {
      if (i)
        foo();
      else
        foo();
    }

With the goal to attach 'nomerge' to both calls done through 'foo':

    @foo = external local_unnamed_addr global ptr, align 8
    define dso_local void @bar(i64 noundef %i) local_unnamed_addr #0 {
      ; ...
      %0 = load ptr, ptr @foo, align 8, !tbaa !5
      ; ...
    if.then:
      tail call void %0() #1
      br label %if.end
    if.else:
      tail call void %0() #1
      br label %if.end
    if.end:
      ret void
    }
    ; ...
    attributes #1 = { nomerge ... }

Report a warning in case if 'nomerge' is specified for a variable that
is not a function pointer, e.g.:

    t.c:2:22: warning: 'nomerge' attribute is ignored because 'j' is not a function pointer [-Wignored-attributes]
        2 | int j __attribute__((nomerge));
          |                      ^

The intended use-case is for BPF backend.

BPF provides a sort of "standard library" functions that are called
helpers. BPF also verifies usage of these helpers before program
execution. Because of limitations of verification / runtime model it
is important to keep calls to some of such helpers from merging.

An example could be found by the link [1], there input C code:

     if (data_end - data > 1024) {
         bpf_for_each_map_elem(&map1, cb, &cb_data, 0);
     } else {
         bpf_for_each_map_elem(&map2, cb, &cb_data, 0);
     }

Is converted to bytecode equivalent to:

     if (data_end - data > 1024)
       tmp = &map1;
     else
       tmp = &map2;
     bpf_for_each_map_elem(tmp, cb, &cb_data, 0);

However, BPF verification/runtime requires to use the same map address
for each particular `bpf_for_each_map_elem()` call.

The 'nomerge' attribute is a perfect match for this situation, but
unfortunately BPF helpers are declared as pointers to functions:

    static long (*bpf_for_each_map_elem)(void *map, ...) = (void *) 164;

Hence, this commit, allowing to use 'nomerge' for function pointers.

[1] https://lore.kernel.org/bpf/03bdf90f-f374-1e67-69d6-76dd9c8318a4@meta.com/

Differential Revision: https://reviews.llvm.org/D152986


  Commit: 6a6db74b77130874f9fd03b366ae77feab746dd7
      https://github.com/llvm/llvm-project/commit/6a6db74b77130874f9fd03b366ae77feab746dd7
  Author: Eduard Zingerman <eddyz87 at gmail.com>
  Date:   2023-06-27 (Tue, 27 Jun 2023)

  Changed paths:
    M llvm/lib/Target/BPF/BPFISelLowering.cpp
    A llvm/test/CodeGen/BPF/no-merge-attr.ll

  Log Message:
  -----------
  [BPF] Propagate NoMerge attribute when lowering function calls

`NoMerge` attribute on machine instructions prevents certain
transformations from merging these instructions.
One of such transformations is 'llvm/lib/CodeGen/BranchFolding.cpp'.

This attribute should be copied from IR `call` instructions to machine
level instructions. See `X86TargetLowering::LowerCall` as another
example.

Differential Revision: https://reviews.llvm.org/D152987


Compare: https://github.com/llvm/llvm-project/compare/d45b1b6bc6a3...6a6db74b7713


More information about the All-commits mailing list