[PATCH] D156172: [clang][CodeGen] Emit annotations for function declarations.

2023-09-06 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl updated this revision to Diff 556089.
brendandahl added a comment.

Rebase and fix unused variable warning.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156172/new/

https://reviews.llvm.org/D156172

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/test/CodeGen/annotations-decl-use-decl.c
  clang/test/CodeGen/annotations-decl-use-define.c
  clang/test/CodeGen/annotations-declaration.c
  clang/test/CodeGen/annotations-global.c
  clang/test/CodeGenCXX/attr-annotate-destructor.cpp
  clang/test/CodeGenCXX/attr-annotate.cpp

Index: clang/test/CodeGenCXX/attr-annotate.cpp
===
--- clang/test/CodeGenCXX/attr-annotate.cpp
+++ clang/test/CodeGenCXX/attr-annotate.cpp
@@ -3,9 +3,9 @@
 //CHECK: @[[STR1:.*]] = private unnamed_addr constant [{{.*}} x i8] c"{{.*}}attr-annotate.cpp\00", section "llvm.metadata"
 //CHECK: @[[STR2:.*]] = private unnamed_addr constant [4 x i8] c"abc\00", align 1
 //CHECK: @[[STR:.*]] = private unnamed_addr constant [5 x i8] c"test\00", section "llvm.metadata"
-//CHECK: @[[ARGS:.*]] = private unnamed_addr constant { i32, ptr, i32 } { i32 9, ptr @[[STR2:.*]], i32 8 }, section "llvm.metadata"
-//CHECK: @[[ARGS2:.*]] = private unnamed_addr constant { %struct.Struct } { %struct.Struct { ptr @_ZN1AIjLj9EE2SVE, ptr getelementptr (i8, ptr @_ZN1AIjLj9EE2SVE, i64 4) } }, section "llvm.metadata"
-//CHECK: @llvm.global.annotations = appending global [2 x { ptr, ptr, ptr, i32, ptr }] [{ ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE4testILi8EEEvv, ptr @[[STR:.*]], ptr @[[STR1:.*]], i32 {{.*}}, ptr @[[ARGS:.*]] }, { ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE5test2Ev, ptr @.str.6, ptr @.str.1, i32 24, ptr @[[ARGS2]] }]
+//CHECK: @[[ARGS:.*]] = private unnamed_addr constant { %struct.Struct } { %struct.Struct { ptr @_ZN1AIjLj9EE2SVE, ptr getelementptr (i8, ptr @_ZN1AIjLj9EE2SVE, i64 4) } }, section "llvm.metadata"
+//CHECK: @[[ARGS2:.*]] = private unnamed_addr constant { i32, ptr, i32 } { i32 9, ptr @[[STR2:.*]], i32 8 }, section "llvm.metadata"
+//CHECK: @llvm.global.annotations = appending global [2 x { ptr, ptr, ptr, i32, ptr }] [{ ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE5test2Ev, ptr @.str.6, ptr @.str.1, i32 24, ptr @[[ARGS]] }, { ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE4testILi8EEEvv, ptr @[[STR:.*]], ptr @[[STR1:.*]], i32 {{.*}}, ptr @[[ARGS2:.*]] }]
 
 constexpr const char* str() {
   return "abc";
Index: clang/test/CodeGenCXX/attr-annotate-destructor.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/attr-annotate-destructor.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 %s -S -emit-llvm -triple x86_64-unknown-linux-gnu -o - | FileCheck %s
+
+// Test annotation attributes on destructors doesn't crash.
+
+struct k {
+  ~k() __attribute__((annotate(""))) {}
+};
+void m() { k(); }
+
+// CHECK: @llvm.global.annotations = appending global [2 x { ptr, ptr, ptr, i32, ptr }] [{
Index: clang/test/CodeGen/annotations-global.c
===
--- clang/test/CodeGen/annotations-global.c
+++ clang/test/CodeGen/annotations-global.c
@@ -33,15 +33,15 @@
 // CHECK: @llvm.global.annotations = appending global [11 x { ptr, ptr, ptr, i32, ptr }] [{
 // CHECK-SAME: { ptr @a.bar,
 // CHECK-SAME: { ptr @a.bar,
-// CHECK-SAME: { ptr @a,
-// CHECK-SAME: { ptr @a,
-// CHECK-SAME: { ptr @a,
-// CHECK-SAME: { ptr @a,
 // CHECK-SAME: { ptr @sfoo,
 // CHECK-SAME: { ptr @sfoo,
 // CHECK-SAME: { ptr @foo,
 // CHECK-SAME: { ptr @foo,
 // CHECK-SAME: { ptr addrspacecast (ptr addrspace(1) @addrspace1_var to ptr),
+// CHECK-SAME: { ptr @a,
+// CHECK-SAME: { ptr @a,
+// CHECK-SAME: { ptr @a,
+// CHECK-SAME: { ptr @a,
 // CHECK-SAME: }], section "llvm.metadata"
 
 // AS1-GLOBALS: target datalayout = "{{.+}}-A5-G1"
Index: clang/test/CodeGen/annotations-declaration.c
===
--- /dev/null
+++ clang/test/CodeGen/annotations-declaration.c
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
+
+// Test annotation attributes are emitted for declarations.
+
+__attribute__((annotate("bar"))) int foo();
+
+int main() {
+  return foo();
+}
+
+// CHECK: target triple
+// CHECK-DAG: private unnamed_addr constant [4 x i8] c"bar\00", section "llvm.metadata"
+
+// CHECK: @llvm.global.annotations = appending global [1 x { ptr, ptr, ptr, i32, ptr }] [{
+// CHECK-SAME: { ptr @foo,
+// CHECK-SAME: }], section "llvm.metadata"
+
Index: clang/test/CodeGen/annotations-decl-use-define.c
===
--- /dev/null
+++ clang/test/CodeGen/annotations-decl-use-define.c
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
+
+// Test annotation attributes are still emitted when the function is used before
+// it is defined with annotations

[PATCH] D156172: [clang][CodeGen] Emit annotations for function declarations.

2023-08-24 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl added a comment.

@efriedma ping


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156172/new/

https://reviews.llvm.org/D156172

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156172: [clang][CodeGen] Emit annotations for function declarations.

2023-08-08 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl added a comment.

@efriedma I've updated the patch to fix the decl-use-decl example and added a 
test for it. Anything else?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156172/new/

https://reviews.llvm.org/D156172

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156172: [clang][CodeGen] Emit annotations for function declarations.

2023-08-03 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl updated this revision to Diff 546986.
brendandahl added a comment.

Cleanup some extra brackets.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156172/new/

https://reviews.llvm.org/D156172

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/test/CodeGen/annotations-decl-use-decl.c
  clang/test/CodeGen/annotations-decl-use-define.c
  clang/test/CodeGen/annotations-declaration.c
  clang/test/CodeGen/annotations-global.c
  clang/test/CodeGenCXX/attr-annotate-destructor.cpp
  clang/test/CodeGenCXX/attr-annotate.cpp

Index: clang/test/CodeGenCXX/attr-annotate.cpp
===
--- clang/test/CodeGenCXX/attr-annotate.cpp
+++ clang/test/CodeGenCXX/attr-annotate.cpp
@@ -3,9 +3,9 @@
 //CHECK: @[[STR1:.*]] = private unnamed_addr constant [{{.*}} x i8] c"{{.*}}attr-annotate.cpp\00", section "llvm.metadata"
 //CHECK: @[[STR2:.*]] = private unnamed_addr constant [4 x i8] c"abc\00", align 1
 //CHECK: @[[STR:.*]] = private unnamed_addr constant [5 x i8] c"test\00", section "llvm.metadata"
-//CHECK: @[[ARGS:.*]] = private unnamed_addr constant { i32, ptr, i32 } { i32 9, ptr @[[STR2:.*]], i32 8 }, section "llvm.metadata"
-//CHECK: @[[ARGS2:.*]] = private unnamed_addr constant { %struct.Struct } { %struct.Struct { ptr @_ZN1AIjLj9EE2SVE, ptr getelementptr (i8, ptr @_ZN1AIjLj9EE2SVE, i64 4) } }, section "llvm.metadata"
-//CHECK: @llvm.global.annotations = appending global [2 x { ptr, ptr, ptr, i32, ptr }] [{ ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE4testILi8EEEvv, ptr @[[STR:.*]], ptr @[[STR1:.*]], i32 {{.*}}, ptr @[[ARGS:.*]] }, { ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE5test2Ev, ptr @.str.6, ptr @.str.1, i32 24, ptr @[[ARGS2]] }]
+//CHECK: @[[ARGS:.*]] = private unnamed_addr constant { %struct.Struct } { %struct.Struct { ptr @_ZN1AIjLj9EE2SVE, ptr getelementptr (i8, ptr @_ZN1AIjLj9EE2SVE, i64 4) } }, section "llvm.metadata"
+//CHECK: @[[ARGS2:.*]] = private unnamed_addr constant { i32, ptr, i32 } { i32 9, ptr @[[STR2:.*]], i32 8 }, section "llvm.metadata"
+//CHECK: @llvm.global.annotations = appending global [2 x { ptr, ptr, ptr, i32, ptr }] [{ ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE5test2Ev, ptr @.str.6, ptr @.str.1, i32 24, ptr @[[ARGS]] }, { ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE4testILi8EEEvv, ptr @[[STR:.*]], ptr @[[STR1:.*]], i32 {{.*}}, ptr @[[ARGS2:.*]] }]
 
 constexpr const char* str() {
   return "abc";
Index: clang/test/CodeGenCXX/attr-annotate-destructor.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/attr-annotate-destructor.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 %s -S -emit-llvm -triple x86_64-unknown-linux-gnu -o - | FileCheck %s
+
+// Test annotation attributes on destructors doesn't crash.
+
+struct k {
+  ~k() __attribute__((annotate(""))) {}
+};
+void m() { k(); }
+
+// CHECK: @llvm.global.annotations = appending global [2 x { ptr, ptr, ptr, i32, ptr }] [{
Index: clang/test/CodeGen/annotations-global.c
===
--- clang/test/CodeGen/annotations-global.c
+++ clang/test/CodeGen/annotations-global.c
@@ -33,15 +33,15 @@
 // CHECK: @llvm.global.annotations = appending global [11 x { ptr, ptr, ptr, i32, ptr }] [{
 // CHECK-SAME: { ptr @a.bar,
 // CHECK-SAME: { ptr @a.bar,
-// CHECK-SAME: { ptr @a,
-// CHECK-SAME: { ptr @a,
-// CHECK-SAME: { ptr @a,
-// CHECK-SAME: { ptr @a,
 // CHECK-SAME: { ptr @sfoo,
 // CHECK-SAME: { ptr @sfoo,
 // CHECK-SAME: { ptr @foo,
 // CHECK-SAME: { ptr @foo,
 // CHECK-SAME: { ptr addrspacecast (ptr addrspace(1) @addrspace1_var to ptr),
+// CHECK-SAME: { ptr @a,
+// CHECK-SAME: { ptr @a,
+// CHECK-SAME: { ptr @a,
+// CHECK-SAME: { ptr @a,
 // CHECK-SAME: }], section "llvm.metadata"
 
 // AS1-GLOBALS: target datalayout = "{{.+}}-A5-G1"
Index: clang/test/CodeGen/annotations-declaration.c
===
--- /dev/null
+++ clang/test/CodeGen/annotations-declaration.c
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
+
+// Test annotation attributes are emitted for declarations.
+
+__attribute__((annotate("bar"))) int foo();
+
+int main() {
+  return foo();
+}
+
+// CHECK: target triple
+// CHECK-DAG: private unnamed_addr constant [4 x i8] c"bar\00", section "llvm.metadata"
+
+// CHECK: @llvm.global.annotations = appending global [1 x { ptr, ptr, ptr, i32, ptr }] [{
+// CHECK-SAME: { ptr @foo,
+// CHECK-SAME: }], section "llvm.metadata"
+
Index: clang/test/CodeGen/annotations-decl-use-define.c
===
--- /dev/null
+++ clang/test/CodeGen/annotations-decl-use-define.c
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
+
+// Test annotation attributes are still emitted when the function is used before
+// it is defined with annotations.
+
+void f

[PATCH] D156172: [clang][CodeGen] Emit annotations for function declarations.

2023-08-03 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl updated this revision to Diff 546966.
brendandahl added a comment.

Update deferred annotations whenever EmitGlobalDefinition is called
with a FunctionDecl and it has already been used or defined.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156172/new/

https://reviews.llvm.org/D156172

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/test/CodeGen/annotations-decl-use-decl.c
  clang/test/CodeGen/annotations-decl-use-define.c
  clang/test/CodeGen/annotations-declaration.c
  clang/test/CodeGen/annotations-global.c
  clang/test/CodeGenCXX/attr-annotate-destructor.cpp
  clang/test/CodeGenCXX/attr-annotate.cpp

Index: clang/test/CodeGenCXX/attr-annotate.cpp
===
--- clang/test/CodeGenCXX/attr-annotate.cpp
+++ clang/test/CodeGenCXX/attr-annotate.cpp
@@ -3,9 +3,9 @@
 //CHECK: @[[STR1:.*]] = private unnamed_addr constant [{{.*}} x i8] c"{{.*}}attr-annotate.cpp\00", section "llvm.metadata"
 //CHECK: @[[STR2:.*]] = private unnamed_addr constant [4 x i8] c"abc\00", align 1
 //CHECK: @[[STR:.*]] = private unnamed_addr constant [5 x i8] c"test\00", section "llvm.metadata"
-//CHECK: @[[ARGS:.*]] = private unnamed_addr constant { i32, ptr, i32 } { i32 9, ptr @[[STR2:.*]], i32 8 }, section "llvm.metadata"
-//CHECK: @[[ARGS2:.*]] = private unnamed_addr constant { %struct.Struct } { %struct.Struct { ptr @_ZN1AIjLj9EE2SVE, ptr getelementptr (i8, ptr @_ZN1AIjLj9EE2SVE, i64 4) } }, section "llvm.metadata"
-//CHECK: @llvm.global.annotations = appending global [2 x { ptr, ptr, ptr, i32, ptr }] [{ ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE4testILi8EEEvv, ptr @[[STR:.*]], ptr @[[STR1:.*]], i32 {{.*}}, ptr @[[ARGS:.*]] }, { ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE5test2Ev, ptr @.str.6, ptr @.str.1, i32 24, ptr @[[ARGS2]] }]
+//CHECK: @[[ARGS:.*]] = private unnamed_addr constant { %struct.Struct } { %struct.Struct { ptr @_ZN1AIjLj9EE2SVE, ptr getelementptr (i8, ptr @_ZN1AIjLj9EE2SVE, i64 4) } }, section "llvm.metadata"
+//CHECK: @[[ARGS2:.*]] = private unnamed_addr constant { i32, ptr, i32 } { i32 9, ptr @[[STR2:.*]], i32 8 }, section "llvm.metadata"
+//CHECK: @llvm.global.annotations = appending global [2 x { ptr, ptr, ptr, i32, ptr }] [{ ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE5test2Ev, ptr @.str.6, ptr @.str.1, i32 24, ptr @[[ARGS]] }, { ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE4testILi8EEEvv, ptr @[[STR:.*]], ptr @[[STR1:.*]], i32 {{.*}}, ptr @[[ARGS2:.*]] }]
 
 constexpr const char* str() {
   return "abc";
Index: clang/test/CodeGenCXX/attr-annotate-destructor.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/attr-annotate-destructor.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 %s -S -emit-llvm -triple x86_64-unknown-linux-gnu -o - | FileCheck %s
+
+// Test annotation attributes on destructors doesn't crash.
+
+struct k {
+  ~k() __attribute__((annotate(""))) {}
+};
+void m() { k(); }
+
+// CHECK: @llvm.global.annotations = appending global [2 x { ptr, ptr, ptr, i32, ptr }] [{
Index: clang/test/CodeGen/annotations-global.c
===
--- clang/test/CodeGen/annotations-global.c
+++ clang/test/CodeGen/annotations-global.c
@@ -33,15 +33,15 @@
 // CHECK: @llvm.global.annotations = appending global [11 x { ptr, ptr, ptr, i32, ptr }] [{
 // CHECK-SAME: { ptr @a.bar,
 // CHECK-SAME: { ptr @a.bar,
-// CHECK-SAME: { ptr @a,
-// CHECK-SAME: { ptr @a,
-// CHECK-SAME: { ptr @a,
-// CHECK-SAME: { ptr @a,
 // CHECK-SAME: { ptr @sfoo,
 // CHECK-SAME: { ptr @sfoo,
 // CHECK-SAME: { ptr @foo,
 // CHECK-SAME: { ptr @foo,
 // CHECK-SAME: { ptr addrspacecast (ptr addrspace(1) @addrspace1_var to ptr),
+// CHECK-SAME: { ptr @a,
+// CHECK-SAME: { ptr @a,
+// CHECK-SAME: { ptr @a,
+// CHECK-SAME: { ptr @a,
 // CHECK-SAME: }], section "llvm.metadata"
 
 // AS1-GLOBALS: target datalayout = "{{.+}}-A5-G1"
Index: clang/test/CodeGen/annotations-declaration.c
===
--- /dev/null
+++ clang/test/CodeGen/annotations-declaration.c
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
+
+// Test annotation attributes are emitted for declarations.
+
+__attribute__((annotate("bar"))) int foo();
+
+int main() {
+  return foo();
+}
+
+// CHECK: target triple
+// CHECK-DAG: private unnamed_addr constant [4 x i8] c"bar\00", section "llvm.metadata"
+
+// CHECK: @llvm.global.annotations = appending global [1 x { ptr, ptr, ptr, i32, ptr }] [{
+// CHECK-SAME: { ptr @foo,
+// CHECK-SAME: }], section "llvm.metadata"
+
Index: clang/test/CodeGen/annotations-decl-use-define.c
===
--- /dev/null
+++ clang/test/CodeGen/annotations-decl-use-define.c
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
+
+// Test annotation attribu

[PATCH] D156172: [clang][CodeGen] Emit annotations for function declarations.

2023-08-02 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl updated this revision to Diff 546632.
brendandahl added a comment.

Switch to emitting annotations and the end.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156172/new/

https://reviews.llvm.org/D156172

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/test/CodeGen/annotations-decl-use-define.c
  clang/test/CodeGen/annotations-declaration.c
  clang/test/CodeGen/annotations-global.c
  clang/test/CodeGenCXX/attr-annotate-destructor.cpp

Index: clang/test/CodeGenCXX/attr-annotate-destructor.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/attr-annotate-destructor.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 %s -S -emit-llvm -triple x86_64-unknown-linux-gnu -o - | FileCheck %s
+
+// Test annotation attributes on destructors doesn't crash.
+
+struct k {
+  ~k() __attribute__((annotate(""))) {}
+};
+void m() { k(); }
+
+// CHECK: @llvm.global.annotations = appending global [2 x { ptr, ptr, ptr, i32, ptr }] [{
Index: clang/test/CodeGen/annotations-global.c
===
--- clang/test/CodeGen/annotations-global.c
+++ clang/test/CodeGen/annotations-global.c
@@ -33,15 +33,15 @@
 // CHECK: @llvm.global.annotations = appending global [11 x { ptr, ptr, ptr, i32, ptr }] [{
 // CHECK-SAME: { ptr @a.bar,
 // CHECK-SAME: { ptr @a.bar,
-// CHECK-SAME: { ptr @a,
-// CHECK-SAME: { ptr @a,
-// CHECK-SAME: { ptr @a,
-// CHECK-SAME: { ptr @a,
 // CHECK-SAME: { ptr @sfoo,
 // CHECK-SAME: { ptr @sfoo,
 // CHECK-SAME: { ptr @foo,
 // CHECK-SAME: { ptr @foo,
 // CHECK-SAME: { ptr addrspacecast (ptr addrspace(1) @addrspace1_var to ptr),
+// CHECK-SAME: { ptr @a,
+// CHECK-SAME: { ptr @a,
+// CHECK-SAME: { ptr @a,
+// CHECK-SAME: { ptr @a,
 // CHECK-SAME: }], section "llvm.metadata"
 
 // AS1-GLOBALS: target datalayout = "{{.+}}-A5-G1"
Index: clang/test/CodeGen/annotations-declaration.c
===
--- /dev/null
+++ clang/test/CodeGen/annotations-declaration.c
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
+
+// Test annotation attributes are emitted for declarations.
+
+__attribute__((annotate("bar"))) int foo();
+
+int main() {
+  return foo();
+}
+
+// CHECK: target triple
+// CHECK-DAG: private unnamed_addr constant [4 x i8] c"bar\00", section "llvm.metadata"
+
+// CHECK: @llvm.global.annotations = appending global [1 x { ptr, ptr, ptr, i32, ptr }] [{
+// CHECK-SAME: { ptr @foo,
+// CHECK-SAME: }], section "llvm.metadata"
+
Index: clang/test/CodeGen/annotations-decl-use-define.c
===
--- /dev/null
+++ clang/test/CodeGen/annotations-decl-use-define.c
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
+
+// Test annotation attributes are still emitted when the function is used before
+// it is defined with annotations.
+
+void foo(void);
+void *xxx = (void*)foo;
+void __attribute__((annotate("bar"))) foo() {}
+
+// CHECK: target triple
+// CHECK-DAG: private unnamed_addr constant [4 x i8] c"bar\00", section "llvm.metadata"
+
+// CHECK: @llvm.global.annotations = appending global [1 x { ptr, ptr, ptr, i32, ptr }] [{
+// CHECK-SAME: { ptr @foo,
+// CHECK-SAME: }], section "llvm.metadata"
+
Index: clang/lib/CodeGen/CodeGenModule.h
===
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -423,6 +423,10 @@
   /// Global annotations.
   std::vector Annotations;
 
+  // Store deferred function annotations so they can be emitted at the end with
+  // most up to date ValueDecl that will have all the inherited annotations.
+  llvm::DenseMap DeferredAnnotations;
+
   /// Map used to get unique annotation strings.
   llvm::StringMap AnnotationStrings;
 
Index: clang/lib/CodeGen/CodeGenModule.cpp
===
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -689,6 +689,7 @@
 void CodeGenModule::clear() {
   DeferredDeclsToEmit.clear();
   EmittedDeferredDecls.clear();
+  DeferredAnnotations.clear();
   if (OpenMPRuntime)
 OpenMPRuntime->clear();
 }
@@ -3078,6 +3079,10 @@
 }
 
 void CodeGenModule::EmitGlobalAnnotations() {
+  for (const auto& [F, VD] : DeferredAnnotations)
+AddGlobalAnnotations(VD, F);
+  DeferredAnnotations.clear();
+
   if (Annotations.empty())
 return;
 
@@ -4316,6 +4321,11 @@
   }
 }
 
+// Store the declaration associated with this function so it is emitted at
+// the end.
+if (D && D->hasAttr() && isa(Entry))
+  DeferredAnnotations[cast(Entry)] = cast(D);
+
 if ((isa(Entry) || isa(Entry)) &&
 (Entry->getValueType() == Ty)) {
   return Entry;
@@ -4346,6 +4356,11 @@
   llvm::Function::Create(FTy, llvm::Function::ExternalLink

[PATCH] D156172: [clang][CodeGen] Emit annotations for function declarations.

2023-07-27 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl added a comment.

I looked into the above issue with mixing declarations and definitions and I 
think I can fix that by leaving the AddGlobalAnnotations call in 
EmitGlobalFunctionDefinition and only calling AddGlobalAnnotations from 
GetOrCreateLLVMFunction when it's for a definition.  However, with that I'm 
running into some duplicate annotations being created with C++ templates that I 
need to fix.

While looking into the duplicate annotations, I noticed someone else already 
implemented annotations for declarations , 
but it was rolled back.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156172/new/

https://reviews.llvm.org/D156172

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156172: [clang][CodeGen] Emit annotations for function declarations.

2023-07-25 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl added a comment.

That example appears to still work the same with my patch:

  @xxx = global ptr @foo, align 8
  @.str = private unnamed_addr constant [4 x i8] c"bar\00", section 
"llvm.metadata"
  @.str.1 = private unnamed_addr constant [8 x i8] c"main2.c\00", section 
"llvm.metadata"
  @llvm.global.annotations = appending global [1 x { ptr, ptr, ptr, i32, ptr }] 
[{ ptr, ptr, ptr, i32, ptr } { ptr @foo, ptr @.str, ptr @.str.1, i32 3, ptr 
null }], section "llvm.metadata"

I'm not very familiar with the CodeGenModule, so I'm happy to move the call to 
`AddGlobalAnnotations` if there is a better place.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156172/new/

https://reviews.llvm.org/D156172

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156172: [clang][CodeGen] Emit annotations for function declarations.

2023-07-25 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl added a comment.

A little more context for the new reviewers. Over in this patch 
 I added support for annotations to be 
emitted into WebAssembly. We'd like to use this to mark imported (declarations) 
and exported (definitions) of functions with special attributes.

In D156172#4531885 , @aaron.ballman 
wrote:

> The changes seem reasonable to me given that the annotation attribute is used 
> to squirrel data from the frontend to the backend (including via plugin 
> attributes) and so its usage is pretty general. But I'd like a second opinion 
> given that this attribute has only passed data along for function definitions 
> for a *long* time. I'm not certain if changing this will cause surprises for 
> the backend.

Interestingly, I noticed the original patch 

 that added annotations had support for merging annotations of declarations and 
definitions.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156172/new/

https://reviews.llvm.org/D156172

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156172: [clang][CodeGen] Emit annotations for function declarations.

2023-07-24 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl created this revision.
Herald added a project: All.
brendandahl requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Previously, annotations were only emitted for function definitions. With
this change annotations are also emitted for declarations. Emitting for
both also more closely matches the behavior of regular function attributes.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156172

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/test/CodeGen/annotations-declaration.c
  clang/test/CodeGen/annotations-global.c
  clang/test/CodeGenCXX/attr-annotate.cpp

Index: clang/test/CodeGenCXX/attr-annotate.cpp
===
--- clang/test/CodeGenCXX/attr-annotate.cpp
+++ clang/test/CodeGenCXX/attr-annotate.cpp
@@ -1,11 +1,11 @@
 // RUN: %clang_cc1 %s -S -emit-llvm -triple x86_64-unknown-linux-gnu -o - | FileCheck %s
 
+//CHECK: @[[STR:.*]] = private unnamed_addr constant [5 x i8] c"test\00", section "llvm.metadata"
 //CHECK: @[[STR1:.*]] = private unnamed_addr constant [{{.*}} x i8] c"{{.*}}attr-annotate.cpp\00", section "llvm.metadata"
 //CHECK: @[[STR2:.*]] = private unnamed_addr constant [4 x i8] c"abc\00", align 1
-//CHECK: @[[STR:.*]] = private unnamed_addr constant [5 x i8] c"test\00", section "llvm.metadata"
 //CHECK: @[[ARGS:.*]] = private unnamed_addr constant { i32, ptr, i32 } { i32 9, ptr @[[STR2:.*]], i32 8 }, section "llvm.metadata"
-//CHECK: @[[ARGS2:.*]] = private unnamed_addr constant { %struct.Struct } { %struct.Struct { ptr @_ZN1AIjLj9EE2SVE, ptr getelementptr (i8, ptr @_ZN1AIjLj9EE2SVE, i64 4) } }, section "llvm.metadata"
-//CHECK: @llvm.global.annotations = appending global [2 x { ptr, ptr, ptr, i32, ptr }] [{ ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE4testILi8EEEvv, ptr @[[STR:.*]], ptr @[[STR1:.*]], i32 {{.*}}, ptr @[[ARGS:.*]] }, { ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE5test2Ev, ptr @.str.6, ptr @.str.1, i32 24, ptr @[[ARGS2]] }]
+//CHECK: @[[ARGS3:.*]] = private unnamed_addr constant { %struct.Struct } { %struct.Struct { ptr @_ZN1AIjLj9EE2SVE, ptr getelementptr (i8, ptr @_ZN1AIjLj9EE2SVE, i64 4) } }, section "llvm.metadata"
+//CHECK: @llvm.global.annotations = appending global [2 x { ptr, ptr, ptr, i32, ptr }] [{ ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE4testILi8EEEvv, ptr @[[STR:.*]], ptr @[[STR1:.*]], i32 {{.*}}, ptr @[[ARGS:.*]] }, { ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE5test2Ev, ptr @.str, ptr @.str.1, i32 24, ptr @[[ARGS3]] }]
 
 constexpr const char* str() {
   return "abc";
@@ -51,12 +51,12 @@
 // CHECK-NEXT:store ptr [[ARGV:%.*]], ptr [[ARGV_ADDR]], align 8
 // CHECK-NEXT:[[TMP0:%.*]] = load i32, ptr [[ARGC_ADDR]], align 4
 // CHECK-NEXT:[[V:%.*]] = getelementptr inbounds %"struct.B::foo", ptr [[F]], i32 0, i32 0
-// CHECK-NEXT:[[TMP2:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[V]], ptr @.str, ptr @.str.1, i32 {{.*}}, ptr @.args)
-// CHECK-NEXT:[[TMP5:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[TMP2]], ptr @.str.3, ptr @.str.1, i32 {{.*}}, ptr @.args.4)
+// CHECK-NEXT:[[TMP2:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[V]], ptr @.str.4, ptr @.str.1, i32 {{.*}}, ptr @.args.5)
+// CHECK-NEXT:[[TMP5:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[TMP2]], ptr @.str.6, ptr @.str.1, i32 {{.*}}, ptr @.args.7)
 // CHECK-NEXT:store i32 [[TMP0]], ptr [[TMP5]], align 4
 // CHECK-NEXT:[[TMP7:%.*]] = load i32, ptr [[ARGC_ADDR]], align 4
-// CHECK-NEXT:[[TMP8:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr @_ZL2gf, ptr @.str, ptr @.str.1, i32 {{.*}}, ptr @.args.5)
-// CHECK-NEXT:[[TMP11:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[TMP8]], ptr @.str.3, ptr @.str.1, i32 {{.*}}, ptr @.args.4)
+// CHECK-NEXT:[[TMP8:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr @_ZL2gf, ptr @.str.4, ptr @.str.1, i32 {{.*}}, ptr @.args.8)
+// CHECK-NEXT:[[TMP11:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[TMP8]], ptr @.str.6, ptr @.str.1, i32 {{.*}}, ptr @.args.7)
 // CHECK-NEXT:store i32 [[TMP7]], ptr [[TMP11]], align 4
 // CHECK-NEXT:ret i32 0
 //
Index: clang/test/CodeGen/annotations-global.c
===
--- clang/test/CodeGen/annotations-global.c
+++ clang/test/CodeGen/annotations-global.c
@@ -31,12 +31,12 @@
 // CHECK-DAG: private unnamed_addr constant [15 x i8] c"addrspace1_ann\00", section "llvm.metadata"
 
 // CHECK: @llvm.global.annotations = appending global [11 x { ptr, ptr, ptr, i32, ptr }] [{
-// CHECK-SAME: { ptr @a.bar,
-// CHECK-SAME: { ptr @a.bar,
 // CHECK-SAME: { ptr @a,
 // CHECK-SAME: { ptr @a,
 // CHECK-SAME: { ptr @a,
 // CHECK-SAME: { ptr @a,
+// CHECK-SAME: { ptr @a.bar,
+// CHECK-SAME: { ptr @a.bar,
 // CHECK-SAME: { ptr @sfoo,
 // CHECK-SAME: { ptr @sfoo,
 // CHECK-SAME: { ptr @foo,
Index: clang/test/CodeGen/annotations-declaration.c
=

[PATCH] D150803: [WebAssembly] Support `annotate` clang attributes for marking functions.

2023-07-11 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl updated this revision to Diff 539191.
brendandahl added a comment.

Rebase on main.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

Files:
  lld/test/wasm/func-attr-tombstone.s
  lld/test/wasm/func-attr.s
  lld/test/wasm/merge-func-attr-section.s
  lld/wasm/InputChunks.cpp
  lld/wasm/InputFiles.cpp
  llvm/docs/ReleaseNotes.rst
  llvm/include/llvm/BinaryFormat/WasmRelocs.def
  llvm/include/llvm/MC/MCExpr.h
  llvm/lib/MC/MCExpr.cpp
  llvm/lib/MC/WasmObjectWriter.cpp
  llvm/lib/Object/WasmObjectFile.cpp
  llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
  llvm/test/CodeGen/WebAssembly/func-attr-annotate.ll
  llvm/test/MC/WebAssembly/func-attr.s

Index: llvm/test/MC/WebAssembly/func-attr.s
===
--- /dev/null
+++ llvm/test/MC/WebAssembly/func-attr.s
@@ -0,0 +1,21 @@
+# RUN: llvm-mc -triple=wasm32-unknown-unknown < %s | FileCheck %s
+# Check that it also comiled to object for format.
+# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj -o - < %s | obj2yaml | FileCheck -check-prefix=CHECK-OBJ %s
+
+foo:
+.globl foo
+.functype foo () -> ()
+end_function
+
+.section.custom_section.llvm.func_attr.custom0,"",@
+.int32  foo@FUNCINDEX
+
+# CHECK:   .section .custom_section.llvm.func_attr.custom0,"",@
+# CHECK-NEXT: .int32  foo@FUNCINDEX
+
+# CHECK-OBJ:- Type:CUSTOM
+# CHECK-OBJ-NEXT: Relocations:
+# CHECK-OBJ-NEXT:- Type:R_WASM_FUNCTION_INDEX_I32
+# CHECK-OBJ-NEXT:  Index:   0
+# CHECK-OBJ-NEXT:  Offset:  0x0
+# CHECK-OBJ-NEXT: Name:llvm.func_attr.custom0
Index: llvm/test/CodeGen/WebAssembly/func-attr-annotate.ll
===
--- /dev/null
+++ llvm/test/CodeGen/WebAssembly/func-attr-annotate.ll
@@ -0,0 +1,31 @@
+; RUN: llc < %s -asm-verbose=false -wasm-keep-registers | FileCheck %s
+
+target triple = "wasm32-unknown-unknown"
+
+@.str = private unnamed_addr constant [8 x i8] c"custom0\00", section "llvm.metadata"
+@.str.1 = private unnamed_addr constant [7 x i8] c"main.c\00", section "llvm.metadata"
+@.str.2 = private unnamed_addr constant [8 x i8] c"custom1\00", section "llvm.metadata"
+@.str.3 = private unnamed_addr constant [8 x i8] c"custom2\00", section "llvm.metadata"
+@llvm.global.annotations = appending global [3 x { ptr, ptr, ptr, i32, ptr }] [{ ptr, ptr, ptr, i32, ptr } { ptr @test0, ptr @.str, ptr @.str.1, i32 4, ptr null }, { ptr, ptr, ptr, i32, ptr } { ptr @test1, ptr @.str, ptr @.str.1, i32 5, ptr null }, { ptr, ptr, ptr, i32, ptr } { ptr @test2, ptr @.str.2, ptr @.str.1, i32 6, ptr null }], section "llvm.metadata"
+
+define void @test0() {
+  ret void
+}
+
+define void @test1() {
+  ret void
+}
+
+define void @test2() {
+  ret void
+}
+
+define void @test3() {
+  ret void
+}
+
+; CHECK:  .section.custom_section.llvm.func_attr.annotate.custom0,"",@
+; CHECK-NEXT: .int32  test0@FUNCINDEX
+; CHECK-NEXT: .int32  test1@FUNCINDEX
+; CHECK:  .section.custom_section.llvm.func_attr.annotate.custom1,"",@
+; CHECK-NEXT: .int32  test2@FUNCINDEX
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
@@ -66,6 +66,7 @@
   void emitEndOfAsmFile(Module &M) override;
   void EmitProducerInfo(Module &M);
   void EmitTargetFeatures(Module &M);
+  void EmitFunctionAttributes(Module &M);
   void emitSymbolType(const MCSymbolWasm *Sym);
   void emitGlobalVariable(const GlobalVariable *GV) override;
   void emitJumpTableInfo() override;
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
@@ -27,6 +27,8 @@
 #include "WebAssemblyTargetMachine.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/Analysis/ValueTracking.h"
 #include "llvm/BinaryFormat/Wasm.h"
 #include "llvm/CodeGen/Analysis.h"
 #include "llvm/CodeGen/AsmPrinter.h"
@@ -438,6 +440,7 @@
 
   EmitProducerInfo(M);
   EmitTargetFeatures(M);
+  EmitFunctionAttributes(M);
 }
 
 void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) {
@@ -556,6 +559,49 @@
   OutStreamer->popSection();
 }
 
+void WebAssemblyAsmPrinter::EmitFunctionAttributes(Module &M) {
+  auto V = M.getNamedGlobal("llvm.global.annotations");
+  if (!V)
+return;
+
+  // Group all the custom attributes by name.
+  StringMap> CustomSections;
+  const ConstantArray *

[PATCH] D150803: [WebAssembly] Support `annotate` clang attributes for marking functions.

2023-06-27 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl marked an inline comment as not done.
brendandahl added inline comments.



Comment at: llvm/test/MC/WebAssembly/func-attr.s:21
+# CHECK-OBJ-NEXT:  Offset:  0x0
+# CHECK-OBJ-NEXT: Name:func_attr.custom0

sbc100 wrote:
> Should these be `llvm.func_attr.custom0` perhaps?
> 
> I also wonder if its makes sense to preserve all of these.  I worry that it 
> could result in production code that contains these extra sections.   Can we 
> figure out way to drop them by default on only preserve them in the cases we 
> care about?
> 
> How about maybe a linker flag?  Something like 
> `--preserve-attributes=async`.. that emcc could inject?
I'm planning to have binaryen remove all of these by default, since they're 
only needed as hints for binaryen.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150803: [WebAssembly] Support `annotate` clang attributes for marking functions.

2023-06-27 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl updated this revision to Diff 535187.
brendandahl added a comment.

Remove annotate arguments. Change name from func_attr to llvm.func_attr.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

Files:
  lld/test/wasm/func-attr-tombstone.s
  lld/test/wasm/func-attr.s
  lld/test/wasm/merge-func-attr-section.s
  lld/wasm/InputChunks.cpp
  lld/wasm/InputFiles.cpp
  llvm/docs/ReleaseNotes.rst
  llvm/include/llvm/BinaryFormat/WasmRelocs.def
  llvm/include/llvm/MC/MCExpr.h
  llvm/lib/MC/MCExpr.cpp
  llvm/lib/MC/WasmObjectWriter.cpp
  llvm/lib/Object/WasmObjectFile.cpp
  llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
  llvm/test/CodeGen/WebAssembly/func-attr-annotate.ll
  llvm/test/MC/WebAssembly/func-attr.s

Index: llvm/test/MC/WebAssembly/func-attr.s
===
--- /dev/null
+++ llvm/test/MC/WebAssembly/func-attr.s
@@ -0,0 +1,21 @@
+# RUN: llvm-mc -triple=wasm32-unknown-unknown < %s | FileCheck %s
+# Check that it also comiled to object for format.
+# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj -o - < %s | obj2yaml | FileCheck -check-prefix=CHECK-OBJ %s
+
+foo:
+.globl foo
+.functype foo () -> ()
+end_function
+
+.section.custom_section.llvm.func_attr.custom0,"",@
+.int32  foo@FUNCINDEX
+
+# CHECK:   .section .custom_section.llvm.func_attr.custom0,"",@
+# CHECK-NEXT: .int32  foo@FUNCINDEX
+
+# CHECK-OBJ:- Type:CUSTOM
+# CHECK-OBJ-NEXT: Relocations:
+# CHECK-OBJ-NEXT:- Type:R_WASM_FUNCTION_INDEX_I32
+# CHECK-OBJ-NEXT:  Index:   0
+# CHECK-OBJ-NEXT:  Offset:  0x0
+# CHECK-OBJ-NEXT: Name:llvm.func_attr.custom0
Index: llvm/test/CodeGen/WebAssembly/func-attr-annotate.ll
===
--- /dev/null
+++ llvm/test/CodeGen/WebAssembly/func-attr-annotate.ll
@@ -0,0 +1,31 @@
+; RUN: llc < %s -asm-verbose=false -wasm-keep-registers | FileCheck %s
+
+target triple = "wasm32-unknown-unknown"
+
+@.str = private unnamed_addr constant [8 x i8] c"custom0\00", section "llvm.metadata"
+@.str.1 = private unnamed_addr constant [7 x i8] c"main.c\00", section "llvm.metadata"
+@.str.2 = private unnamed_addr constant [8 x i8] c"custom1\00", section "llvm.metadata"
+@.str.3 = private unnamed_addr constant [8 x i8] c"custom2\00", section "llvm.metadata"
+@llvm.global.annotations = appending global [3 x { ptr, ptr, ptr, i32, ptr }] [{ ptr, ptr, ptr, i32, ptr } { ptr @test0, ptr @.str, ptr @.str.1, i32 4, ptr null }, { ptr, ptr, ptr, i32, ptr } { ptr @test1, ptr @.str, ptr @.str.1, i32 5, ptr null }, { ptr, ptr, ptr, i32, ptr } { ptr @test2, ptr @.str.2, ptr @.str.1, i32 6, ptr null }], section "llvm.metadata"
+
+define void @test0() {
+  ret void
+}
+
+define void @test1() {
+  ret void
+}
+
+define void @test2() {
+  ret void
+}
+
+define void @test3() {
+  ret void
+}
+
+; CHECK:  .section.custom_section.llvm.func_attr.annotate.custom0,"",@
+; CHECK-NEXT: .int32  test0@FUNCINDEX
+; CHECK-NEXT: .int32  test1@FUNCINDEX
+; CHECK:  .section.custom_section.llvm.func_attr.annotate.custom1,"",@
+; CHECK-NEXT: .int32  test2@FUNCINDEX
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
@@ -66,6 +66,7 @@
   void emitEndOfAsmFile(Module &M) override;
   void EmitProducerInfo(Module &M);
   void EmitTargetFeatures(Module &M);
+  void EmitFunctionAttributes(Module &M);
   void emitSymbolType(const MCSymbolWasm *Sym);
   void emitGlobalVariable(const GlobalVariable *GV) override;
   void emitJumpTableInfo() override;
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
@@ -27,6 +27,8 @@
 #include "WebAssemblyTargetMachine.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/Analysis/ValueTracking.h"
 #include "llvm/BinaryFormat/Wasm.h"
 #include "llvm/CodeGen/Analysis.h"
 #include "llvm/CodeGen/AsmPrinter.h"
@@ -438,6 +440,7 @@
 
   EmitProducerInfo(M);
   EmitTargetFeatures(M);
+  EmitFunctionAttributes(M);
 }
 
 void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) {
@@ -556,6 +559,49 @@
   OutStreamer->popSection();
 }
 
+void WebAssemblyAsmPrinter::EmitFunctionAttributes(Module &M) {
+  auto V = M.getNamedGlobal("llvm.global.annotations");
+  if (!V)
+return;
+
+  // Group all the custom attributes by nam

[PATCH] D150803: [WebAssembly] Support `annotate` clang attributes for marking functions.

2023-06-27 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl added inline comments.



Comment at: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp:585-598
+// The fifth field is an optional pointer to the arguments. Append each
+// argument to the name separated by a '.'.
+auto *ArgsVar = 
dyn_cast(CS->getOperand(4)->stripPointerCasts());
+if (ArgsVar) {
+  auto *ArgsStruct = cast(ArgsVar->getInitializer());
+  for (Value *ArgOp : ArgsStruct->operands()) {
+StringRef Arg;

This currently supports arguments, but it's somewhat brittle since the 
arguments aren't escaped. I'm fine with dropping this and only supporting name, 
but I think this could be useful.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150803: Add a new `wasm_custom` clang attribute for marking functions.

2023-06-27 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl added a comment.

In D150803#4440802 , @aaron.ballman 
wrote:

> Marking as requested changes so it's clear there's more worth discussing, so 
> we don't accidentally land this.

I've switched to using `annotate` now. Let me know if there's anything else.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150803: Add a new `wasm_custom` clang attribute for marking functions.

2023-06-27 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl updated this revision to Diff 535126.
brendandahl added a comment.

Use the annotate attribute to generate custom sections.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

Files:
  lld/test/wasm/func-attr-tombstone.s
  lld/test/wasm/func-attr.s
  lld/test/wasm/merge-func-attr-section.s
  lld/wasm/InputChunks.cpp
  lld/wasm/InputFiles.cpp
  llvm/docs/ReleaseNotes.rst
  llvm/include/llvm/BinaryFormat/WasmRelocs.def
  llvm/include/llvm/MC/MCExpr.h
  llvm/lib/MC/MCExpr.cpp
  llvm/lib/MC/WasmObjectWriter.cpp
  llvm/lib/Object/WasmObjectFile.cpp
  llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
  llvm/test/CodeGen/WebAssembly/func-attr-annotate.ll
  llvm/test/MC/WebAssembly/func-attr.s

Index: llvm/test/MC/WebAssembly/func-attr.s
===
--- /dev/null
+++ llvm/test/MC/WebAssembly/func-attr.s
@@ -0,0 +1,21 @@
+# RUN: llvm-mc -triple=wasm32-unknown-unknown < %s | FileCheck %s
+# Check that it also comiled to object for format.
+# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj -o - < %s | obj2yaml | FileCheck -check-prefix=CHECK-OBJ %s
+
+foo:
+.globl foo
+.functype foo () -> ()
+end_function
+
+.section.custom_section.func_attr.custom0,"",@
+.int32  foo@FUNCINDEX
+
+# CHECK:   .section .custom_section.func_attr.custom0,"",@
+# CHECK-NEXT: .int32  foo@FUNCINDEX
+
+# CHECK-OBJ:- Type:CUSTOM
+# CHECK-OBJ-NEXT: Relocations:
+# CHECK-OBJ-NEXT:- Type:R_WASM_FUNCTION_INDEX_I32
+# CHECK-OBJ-NEXT:  Index:   0
+# CHECK-OBJ-NEXT:  Offset:  0x0
+# CHECK-OBJ-NEXT: Name:func_attr.custom0
Index: llvm/test/CodeGen/WebAssembly/func-attr-annotate.ll
===
--- /dev/null
+++ llvm/test/CodeGen/WebAssembly/func-attr-annotate.ll
@@ -0,0 +1,36 @@
+; RUN: llc < %s -asm-verbose=false -wasm-keep-registers | FileCheck %s
+
+target triple = "wasm32-unknown-unknown"
+
+@.str = private unnamed_addr constant [8 x i8] c"custom0\00", section "llvm.metadata"
+@.str.1 = private unnamed_addr constant [7 x i8] c"main.c\00", section "llvm.metadata"
+@.str.2 = private unnamed_addr constant [8 x i8] c"custom1\00", section "llvm.metadata"
+@.str.3 = private unnamed_addr constant [8 x i8] c"custom2\00", section "llvm.metadata"
+@.str.4 = private unnamed_addr constant [12 x i8] c"custom2arg0\00", align 1
+@.str.5 = private unnamed_addr constant [12 x i8] c"custom2arg1\00", align 1
+@.args = private unnamed_addr constant { ptr, ptr } { ptr @.str.4, ptr @.str.5 }, section "llvm.metadata"
+@llvm.global.annotations = appending global [4 x { ptr, ptr, ptr, i32, ptr }] [{ ptr, ptr, ptr, i32, ptr } { ptr @test0, ptr @.str, ptr @.str.1, i32 4, ptr null }, { ptr, ptr, ptr, i32, ptr } { ptr @test1, ptr @.str, ptr @.str.1, i32 5, ptr null }, { ptr, ptr, ptr, i32, ptr } { ptr @test2, ptr @.str.2, ptr @.str.1, i32 6, ptr null }, { ptr, ptr, ptr, i32, ptr } { ptr @test3, ptr @.str.3, ptr @.str.1, i32 7, ptr @.args }], section "llvm.metadata"
+
+define void @test0() {
+  ret void
+}
+
+define void @test1() {
+  ret void
+}
+
+define void @test2() {
+  ret void
+}
+
+define void @test3() {
+  ret void
+}
+
+; CHECK:  .section.custom_section.func_attr.annotate.custom2.custom2arg0.custom2arg1,"",@
+; CHECK-NEXT: .int32  test3@FUNCINDEX
+; CHECK:  .section.custom_section.func_attr.annotate.custom0,"",@
+; CHECK-NEXT: .int32  test0@FUNCINDEX
+; CHECK-NEXT: .int32  test1@FUNCINDEX
+; CHECK:  .section.custom_section.func_attr.annotate.custom1,"",@
+; CHECK-NEXT: .int32  test2@FUNCINDEX
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
@@ -66,6 +66,7 @@
   void emitEndOfAsmFile(Module &M) override;
   void EmitProducerInfo(Module &M);
   void EmitTargetFeatures(Module &M);
+  void EmitFunctionAttributes(Module &M);
   void emitSymbolType(const MCSymbolWasm *Sym);
   void emitGlobalVariable(const GlobalVariable *GV) override;
   void emitJumpTableInfo() override;
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
@@ -27,6 +27,8 @@
 #include "WebAssemblyTargetMachine.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/Analysis/ValueTracking.h"
 #include "llvm/BinaryFormat/Wasm.h"
 #include "llvm/CodeGen/Analysis.h"

[PATCH] D150803: Add a new `wasm_custom` clang attribute for marking functions.

2023-06-22 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl added inline comments.



Comment at: clang/include/clang/Basic/AttrDocs.td:5608-5612
+Clang supports the ``__attribute__((wasm_async))``
+attribute for the WebAssembly target. This attribute may be attached to a
+function definition, which indicates the function will be used with JavaScript
+promise integration (JSPI). The attribute will cause the creation of a custom
+section named "async" that contains each wasm_async function's index value.

sbc100 wrote:
> sbc100 wrote:
> > sbc100 wrote:
> > > aaron.ballman wrote:
> > > > brendandahl wrote:
> > > > > aaron.ballman wrote:
> > > > > > aaron.ballman wrote:
> > > > > > > This could be my ignorance of web assembly showing, but the docs 
> > > > > > > don't really help me understand when you'd want to use this 
> > > > > > > attribute. Perhaps a link to what JSPI is and a code example 
> > > > > > > would help a little bit? Or is this more of a low-level 
> > > > > > > implementation detail kind of attribute where folks already know 
> > > > > > > the domain?
> > > > > > Based on the documentation here, I'm wondering why the `annotate` 
> > > > > > attribute doesn't suffice? That attribute lets you specify custom 
> > > > > > information to associate with a declaration that then gets lowered 
> > > > > > such that passes can do whatever they want with the info, which 
> > > > > > seems to be a more generalized version of what this attribute is.
> > > > > > 
> > > > > > (FWIW, I'm back to having basically no idea when you'd use this 
> > > > > > attribute or what it would be used for, so my thoughts above might 
> > > > > > make no sense.)
> > > > > I was considering that, but it would require more machinery in the 
> > > > > wasm backend to store all the attribute values in the output. For now 
> > > > > we only really need a flag associated with function. I think if we 
> > > > > find more uses for the annotations in the future we could replace 
> > > > > wasm_custom with it.
> > > > > I was considering that, but it would require more machinery in the 
> > > > > wasm backend to store all the attribute values in the output. For now 
> > > > > we only really need a flag associated with function. I think if we 
> > > > > find more uses for the annotations in the future we could replace 
> > > > > wasm_custom with it.
> > > > 
> > > > More machinery in the backend is preferred to exposing a new attribute 
> > > > that is this general-purpose; the backend is what needs this 
> > > > functionality, the frontend basically does nothing with it. (I'm 
> > > > assuming this is an implementation detail attribute and not something 
> > > > you expect users to write. If I'm wrong about that, please let me know.)
> > > > Based on the documentation here, I'm wondering why the `annotate` 
> > > > attribute doesn't suffice? 
> > > 
> > > I believe that was the original solution that was considered but Benden 
> > > was told this was perhaps not an appropriate use of "annotate".   Brenden 
> > > can you elaborate on why?   IIRC it was something like "the semantics of 
> > > the program should not depend on annotate attributes but the attribute 
> > > being added in this case is required for the program to run correctly".. 
> > > is that about right?
> > > 
> > > FWIW, I think using the existing `annotate` attribute would make a lot of 
> > > sense... if we can get away with it.
> > > (I'm assuming this is an implementation detail attribute and not 
> > > something you expect users to write. If I'm wrong about that, please let 
> > > me know.)
> > 
> > IIUC user would indeed be specifying these attributes.   Kind of like they 
> > do for "visibility" today.   The initial attribute that motivated this 
> > change is "async" which tells the host runtime that a certain function 
> > import or export behaves in an async manor (See https://v8.dev/blog/jspi 
> > for more details).
> > > I was considering that, but it would require more machinery in the wasm 
> > > backend to store all the attribute values in the output. For now we only 
> > > really need a flag associated with function. I think if we find more uses 
> > > for the annotations in the future we could replace wasm_custom with it.
> > 
> > More machinery in the backend is preferred to exposing a new attribute that 
> > is this general-purpose; the backend is what needs this functionality, the 
> > frontend basically does nothing with it. (I'm assuming this is an 
> > implementation detail attribute and not something you expect users to 
> > write. If I'm wrong about that, please let me know.)
> 
> I think perhaps there are two alternative implementations being proposed 
> here, and I want to make sure we don't confuse them:
> 
> 1. Use the existing `annotate` attribute.  
> 2. Make an even more complex custom attribute that hold key=value pairs 
> rather than the current CL which proposes simply boolean custom attributes 
> (e.g. key=true).
> 
> I think we would be happy with

[PATCH] D150803: Add a new `wasm_custom` clang attribute for marking functions.

2023-06-21 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl added inline comments.



Comment at: clang/include/clang/Basic/AttrDocs.td:5608-5612
+Clang supports the ``__attribute__((wasm_async))``
+attribute for the WebAssembly target. This attribute may be attached to a
+function definition, which indicates the function will be used with JavaScript
+promise integration (JSPI). The attribute will cause the creation of a custom
+section named "async" that contains each wasm_async function's index value.

aaron.ballman wrote:
> aaron.ballman wrote:
> > This could be my ignorance of web assembly showing, but the docs don't 
> > really help me understand when you'd want to use this attribute. Perhaps a 
> > link to what JSPI is and a code example would help a little bit? Or is this 
> > more of a low-level implementation detail kind of attribute where folks 
> > already know the domain?
> Based on the documentation here, I'm wondering why the `annotate` attribute 
> doesn't suffice? That attribute lets you specify custom information to 
> associate with a declaration that then gets lowered such that passes can do 
> whatever they want with the info, which seems to be a more generalized 
> version of what this attribute is.
> 
> (FWIW, I'm back to having basically no idea when you'd use this attribute or 
> what it would be used for, so my thoughts above might make no sense.)
I was considering that, but it would require more machinery in the wasm backend 
to store all the attribute values in the output. For now we only really need a 
flag associated with function. I think if we find more uses for the annotations 
in the future we could replace wasm_custom with it.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150803: Add a new `wasm_custom` clang attribute for marking functions.

2023-06-20 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl added a comment.

@aaron.ballman or @erichkeane Did you want to re-review after that latest 
changes (more generic attribute) or are things good to go?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150803: Add a new `wasm_custom` clang attribute for marking functions.

2023-06-14 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl updated this revision to Diff 531390.
brendandahl marked an inline comment as done.
brendandahl added a comment.

Review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/lib/CodeGen/TargetInfo.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/CodeGen/WebAssembly/wasm-custom-attr.c
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/Sema/attr-wasm-custom.c
  clang/test/Sema/wasm-custom-support.c
  lld/test/wasm/custom-attr-tombstone.s
  lld/test/wasm/custom-attr.s
  lld/test/wasm/merge-custom-attr-section.s
  lld/wasm/InputChunks.cpp
  lld/wasm/InputFiles.cpp
  llvm/include/llvm/BinaryFormat/WasmRelocs.def
  llvm/include/llvm/MC/MCExpr.h
  llvm/lib/MC/MCExpr.cpp
  llvm/lib/MC/WasmObjectWriter.cpp
  llvm/lib/Object/WasmObjectFile.cpp
  llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
  llvm/test/CodeGen/WebAssembly/custom-attr.ll
  llvm/test/MC/WebAssembly/custom-attr.s

Index: llvm/test/MC/WebAssembly/custom-attr.s
===
--- /dev/null
+++ llvm/test/MC/WebAssembly/custom-attr.s
@@ -0,0 +1,21 @@
+# RUN: llvm-mc -triple=wasm32-unknown-unknown < %s | FileCheck %s
+# Check that it also comiled to object for format.
+# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj -o - < %s | obj2yaml | FileCheck -check-prefix=CHECK-OBJ %s
+
+foo:
+.globl foo
+.functype foo () -> ()
+end_function
+
+.section.custom_section.func_attr.custom0,"",@
+.int32  foo@FUNCINDEX
+
+# CHECK:   .section .custom_section.func_attr.custom0,"",@
+# CHECK-NEXT: .int32  foo@FUNCINDEX
+
+# CHECK-OBJ:- Type:CUSTOM
+# CHECK-OBJ-NEXT: Relocations:
+# CHECK-OBJ-NEXT:- Type:R_WASM_FUNCTION_INDEX_I32
+# CHECK-OBJ-NEXT:  Index:   0
+# CHECK-OBJ-NEXT:  Offset:  0x0
+# CHECK-OBJ-NEXT: Name:func_attr.custom0
Index: llvm/test/CodeGen/WebAssembly/custom-attr.ll
===
--- /dev/null
+++ llvm/test/CodeGen/WebAssembly/custom-attr.ll
@@ -0,0 +1,24 @@
+; RUN: llc < %s -asm-verbose=false -wasm-keep-registers | FileCheck %s
+
+target triple = "wasm32-unknown-unknown"
+
+define void @test0() #0 {
+  ret void
+}
+
+define void @test1() #0 {
+  ret void
+}
+
+define void @test3() #1 {
+  ret void
+}
+
+attributes #0 = { "wasm-custom"="custom0" }
+attributes #1 = { "wasm-custom"="custom1" }
+
+; CHECK:  .section.custom_section.func_attr.custom0,"",@
+; CHECK-NEXT: .int32  test0@FUNCINDEX
+; CHECK-NEXT: .int32  test1@FUNCINDEX
+; CHECK:  .section.custom_section.func_attr.custom1,"",@
+; CHECK-NEXT: .int32  test3@FUNCINDEX
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
@@ -66,6 +66,7 @@
   void emitEndOfAsmFile(Module &M) override;
   void EmitProducerInfo(Module &M);
   void EmitTargetFeatures(Module &M);
+  void EmitCustomFunctionAttributes(Module &M);
   void emitSymbolType(const MCSymbolWasm *Sym);
   void emitGlobalVariable(const GlobalVariable *GV) override;
   void emitJumpTableInfo() override;
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
@@ -25,6 +25,7 @@
 #include "WebAssemblyRegisterInfo.h"
 #include "WebAssemblyRuntimeLibcallSignatures.h"
 #include "WebAssemblyTargetMachine.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/BinaryFormat/Wasm.h"
@@ -438,6 +439,7 @@
 
   EmitProducerInfo(M);
   EmitTargetFeatures(M);
+  EmitCustomFunctionAttributes(M);
 }
 
 void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) {
@@ -556,6 +558,34 @@
   OutStreamer->popSection();
 }
 
+void WebAssemblyAsmPrinter::EmitCustomFunctionAttributes(Module &M) {
+  DenseMap> CustomSections;
+  // Group all the custom attributes by name.
+  for (const auto &F : M) {
+auto *Sym = cast(getSymbol(&F));
+if (F.hasFnAttribute("wasm-custom")) {
+  StringRef Name = F.getFnAttribute("wasm-custom").getValueAsString();
+  CustomSections[Name].push_back(Sym);
+}
+  }
+
+  // Emit a custom section for each unique attribute.
+  for (auto [Name, Symbols] : CustomSections) {
+MCSectionWasm *CustomSection = OutContext.getWasmSection(
+".custom_section.fu

[PATCH] D150803: Add a new `wasm_custom` clang attribute for marking functions.

2023-06-14 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl marked 4 inline comments as done.
brendandahl added inline comments.



Comment at: lld/test/wasm/custom-undefine.s:17
+.type   bar,@function
+bar:
+.functype   bar () -> ()

dschuff wrote:
> I don't fully understand how this test is different from custom-attr.s. Bar 
> is still defined, so the only thing I can see that's different is that it's 
> not called. Is it relying on the linker GCing bar so that it's not defined? 
> is the result different when bar is really undefined?
Undefined is a bad name. I'll change to unreferenced or something. This tests 
when the function isn't referenced a tombstone will be placed in the custom 
section (e.g. the payload below has a ).



Comment at: lld/test/wasm/merge-custom-attr-section.ll:47
+}
+
+attributes #0 = { "wasm-custom"="custom0" }

sbc100 wrote:
> Is there some reason we can't use assembly for this test?
Nope, I rewrote the other two, but missed this one. I'll fix.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150803: Add a new `wasm_custom` clang attribute for marking functions.

2023-06-13 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl updated this revision to Diff 531014.
brendandahl added a comment.

Fix few remaining issues.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/lib/CodeGen/TargetInfo.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/CodeGen/WebAssembly/wasm-custom-attr.c
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/Sema/attr-wasm-custom.c
  clang/test/Sema/wasm-custom-support.c
  lld/test/wasm/custom-attr.s
  lld/test/wasm/custom-undefine.s
  lld/test/wasm/merge-custom-attr-section.ll
  lld/wasm/InputChunks.cpp
  lld/wasm/InputFiles.cpp
  llvm/include/llvm/BinaryFormat/WasmRelocs.def
  llvm/include/llvm/MC/MCExpr.h
  llvm/lib/MC/MCExpr.cpp
  llvm/lib/MC/WasmObjectWriter.cpp
  llvm/lib/Object/WasmObjectFile.cpp
  llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
  llvm/test/CodeGen/WebAssembly/custom-attr.ll
  llvm/test/MC/WebAssembly/custom-attr.s

Index: llvm/test/MC/WebAssembly/custom-attr.s
===
--- /dev/null
+++ llvm/test/MC/WebAssembly/custom-attr.s
@@ -0,0 +1,21 @@
+# RUN: llvm-mc -triple=wasm32-unknown-unknown < %s | FileCheck %s
+# Check that it also comiled to object for format.
+# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj -o - < %s | obj2yaml | FileCheck -check-prefix=CHECK-OBJ %s
+
+foo:
+.globl foo
+.functype foo () -> ()
+end_function
+
+.section.custom_section.func_attr.custom0,"",@
+.int32  foo@FUNCINDEX
+
+# CHECK:   .section .custom_section.func_attr.custom0,"",@
+# CHECK-NEXT: .int32  foo@FUNCINDEX
+
+# CHECK-OBJ:- Type:CUSTOM
+# CHECK-OBJ-NEXT: Relocations:
+# CHECK-OBJ-NEXT:- Type:R_WASM_FUNCTION_INDEX_I32
+# CHECK-OBJ-NEXT:  Index:   0
+# CHECK-OBJ-NEXT:  Offset:  0x0
+# CHECK-OBJ-NEXT: Name:func_attr.custom0
Index: llvm/test/CodeGen/WebAssembly/custom-attr.ll
===
--- /dev/null
+++ llvm/test/CodeGen/WebAssembly/custom-attr.ll
@@ -0,0 +1,24 @@
+; RUN: llc < %s -asm-verbose=false -wasm-keep-registers | FileCheck %s
+
+target triple = "wasm32-unknown-unknown"
+
+define void @test0() #0 {
+  ret void
+}
+
+define void @test1() #0 {
+  ret void
+}
+
+define void @test3() #1 {
+  ret void
+}
+
+attributes #0 = { "wasm-custom"="custom0" }
+attributes #1 = { "wasm-custom"="custom1" }
+
+; CHECK:  .section.custom_section.func_attr.custom0,"",@
+; CHECK-NEXT: .int32  test0@FUNCINDEX
+; CHECK-NEXT: .int32  test1@FUNCINDEX
+; CHECK:  .section.custom_section.func_attr.custom1,"",@
+; CHECK-NEXT: .int32  test3@FUNCINDEX
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
@@ -66,6 +66,7 @@
   void emitEndOfAsmFile(Module &M) override;
   void EmitProducerInfo(Module &M);
   void EmitTargetFeatures(Module &M);
+  void EmitCustomFunctionAttributes(Module &M);
   void emitSymbolType(const MCSymbolWasm *Sym);
   void emitGlobalVariable(const GlobalVariable *GV) override;
   void emitJumpTableInfo() override;
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
@@ -25,6 +25,7 @@
 #include "WebAssemblyRegisterInfo.h"
 #include "WebAssemblyRuntimeLibcallSignatures.h"
 #include "WebAssemblyTargetMachine.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/BinaryFormat/Wasm.h"
@@ -438,6 +439,7 @@
 
   EmitProducerInfo(M);
   EmitTargetFeatures(M);
+  EmitCustomFunctionAttributes(M);
 }
 
 void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) {
@@ -556,6 +558,36 @@
   OutStreamer->popSection();
 }
 
+void WebAssemblyAsmPrinter::EmitCustomFunctionAttributes(Module &M) {
+  DenseMap> CustomSections;
+  // Group all the custom attributes by name.
+  for (const auto &F : M) {
+auto *Sym = cast(getSymbol(&F));
+if (F.hasFnAttribute("wasm-custom")) {
+  StringRef Name = F.getFnAttribute("wasm-custom").getValueAsString();
+  if (!CustomSections.contains(Name))
+CustomSections[Name] = {};
+  CustomSections[Name].push_back(Sym);
+}
+  }
+
+  // Emit a custom section for each unique attribute.
+  for (auto [Name, Symbols] : CustomSections) {
+MCSectionWasm *CustomSection = OutContext.getWasm

[PATCH] D150803: [WebAssembly] Add a new `wasm_async` clang attribute for marking async functions.

2023-06-13 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl updated this revision to Diff 531008.
brendandahl added a comment.

After some feedback, a few people have indicated they'd like to do something 
similar to this, but for their own attributes. I've changed to a more generic 
attribute that allows arbitrary strings. Now a custom section will be created 
for each unique attribute.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/lib/CodeGen/TargetInfo.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/CodeGen/WebAssembly/wasm-custom-attr.c
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/Sema/attr-wasm-custom.c
  clang/test/Sema/wasm-custom-support.c
  lld/test/wasm/custom-attr.s
  lld/test/wasm/custom-undefine.s
  lld/test/wasm/merge-custom-attr-section.ll
  lld/wasm/InputChunks.cpp
  lld/wasm/InputFiles.cpp
  llvm/include/llvm/BinaryFormat/WasmRelocs.def
  llvm/include/llvm/MC/MCExpr.h
  llvm/lib/MC/MCExpr.cpp
  llvm/lib/MC/WasmObjectWriter.cpp
  llvm/lib/Object/WasmObjectFile.cpp
  llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
  llvm/test/CodeGen/WebAssembly/custom-attr.ll
  llvm/test/MC/WebAssembly/custom-attr.s

Index: llvm/test/MC/WebAssembly/custom-attr.s
===
--- /dev/null
+++ llvm/test/MC/WebAssembly/custom-attr.s
@@ -0,0 +1,21 @@
+# RUN: llvm-mc -triple=wasm32-unknown-unknown < %s | FileCheck %s
+# Check that it also comiled to object for format.
+# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj -o - < %s | obj2yaml | FileCheck -check-prefix=CHECK-OBJ %s
+
+foo:
+.globl foo
+.functype foo () -> ()
+end_function
+
+.section.custom_section.async,"",@
+.int32  foo@FUNCINDEX
+
+# CHECK:   .section .custom_section.async,"",@
+# CHECK-NEXT: .int32  foo@FUNCINDEX
+
+# CHECK-OBJ:- Type:CUSTOM
+# CHECK-OBJ-NEXT: Relocations:
+# CHECK-OBJ-NEXT:- Type:R_WASM_FUNCTION_INDEX_I32
+# CHECK-OBJ-NEXT:  Index:   0
+# CHECK-OBJ-NEXT:  Offset:  0x0
+# CHECK-OBJ-NEXT: Name:async
Index: llvm/test/CodeGen/WebAssembly/custom-attr.ll
===
--- /dev/null
+++ llvm/test/CodeGen/WebAssembly/custom-attr.ll
@@ -0,0 +1,24 @@
+; RUN: llc < %s -asm-verbose=false -wasm-keep-registers | FileCheck %s
+
+target triple = "wasm32-unknown-unknown"
+
+define void @test0() #0 {
+  ret void
+}
+
+define void @test1() #0 {
+  ret void
+}
+
+define void @test3() #1 {
+  ret void
+}
+
+attributes #0 = { "wasm-custom"="custom0" }
+attributes #1 = { "wasm-custom"="custom1" }
+
+; CHECK:  .section.custom_section.func_attr.custom0,"",@
+; CHECK-NEXT: .int32  test0@FUNCINDEX
+; CHECK-NEXT: .int32  test1@FUNCINDEX
+; CHECK:  .section.custom_section.func_attr.custom1,"",@
+; CHECK-NEXT: .int32  test3@FUNCINDEX
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
@@ -66,6 +66,7 @@
   void emitEndOfAsmFile(Module &M) override;
   void EmitProducerInfo(Module &M);
   void EmitTargetFeatures(Module &M);
+  void EmitCustomFunctionAttributes(Module &M);
   void emitSymbolType(const MCSymbolWasm *Sym);
   void emitGlobalVariable(const GlobalVariable *GV) override;
   void emitJumpTableInfo() override;
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
@@ -25,6 +25,7 @@
 #include "WebAssemblyRegisterInfo.h"
 #include "WebAssemblyRuntimeLibcallSignatures.h"
 #include "WebAssemblyTargetMachine.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/BinaryFormat/Wasm.h"
@@ -438,6 +439,7 @@
 
   EmitProducerInfo(M);
   EmitTargetFeatures(M);
+  EmitCustomFunctionAttributes(M);
 }
 
 void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) {
@@ -556,6 +558,36 @@
   OutStreamer->popSection();
 }
 
+void WebAssemblyAsmPrinter::EmitCustomFunctionAttributes(Module &M) {
+  DenseMap> CustomSections;
+  // Group all the custom attributes by name.
+  for (const auto &F : M) {
+auto *Sym = cast(getSymbol(&F));
+if (F.hasFnAttribute("wasm-custom")) {
+  StringRef Name = F.getFnAttribute("wasm-custom").getValueAsString();
+  if (!CustomSections.contains(Name))
+CustomSections[Name] = {};
+  CustomSe

[PATCH] D150803: [WebAssembly] Add a new `wasm_async` clang attribute for marking async functions.

2023-06-06 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl marked an inline comment as done.
brendandahl added a comment.

In D150803#4389062 , @sunfish wrote:

> In D150803#4389040 , @dschuff wrote:
>
>> Hm, this is interesting because in the long term we plan to have stack 
>> switching in wasm, which could allow for similar async behavior that JSPI 
>> has, and could be useful in non-web systems. But that's a ways off. The file 
>> format we are generating with this CL will be used in emscripten sooner (and 
>> we may want to try to stabilize it some point, possibly before pure wasm 
>> stack switching is usable in non-web systems).
>> So overall I kind of feel like I could go either way on this. Curious if 
>> @sunfish has had any thoughts about async outside of emscripten.
>
> When we do have stack switching, would you anticipate still using this 
> `wasm_async` attribute, or would we switch to something else at that point?

It's unclear to me at the moment how we're going to implement full stack 
switching with emscripten/llvm. I'm not attached to the `wasm_async` name, so 
we could be more specific and use `wasm_jspi` or something else?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150803: [WebAssembly] Add a new `wasm_async` clang attribute for marking async functions.

2023-06-06 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl marked 2 inline comments as done.
brendandahl added inline comments.



Comment at: clang/lib/Sema/SemaDeclAttr.cpp:7638
+  D->addAttr(::new (S.Context) WebAssemblyAsyncAttr(S.Context, AL));
+  D->addAttr(UsedAttr::CreateImplicit(S.Context));
+}

erichkeane wrote:
> We should probably document that this forces the function to be emitted as 
> well.
I've removed this since we won't actually always want these functions 
preserved. I've changed the linker to now put a tombstone in the async section 
instead. I also added a new test for this.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150803: [WebAssembly] Add a new `wasm_async` clang attribute for marking async functions.

2023-06-06 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl updated this revision to Diff 529028.
brendandahl added a comment.

Review comments. Add tombstone for unused functions.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/lib/CodeGen/TargetInfo.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/CodeGen/WebAssembly/wasm-async.c
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/Sema/attr-wasm-async.c
  clang/test/Sema/wasm-async-support.c
  lld/test/wasm/async-undefined.s
  lld/test/wasm/async.s
  lld/test/wasm/merge-async-section.ll
  lld/wasm/InputChunks.cpp
  lld/wasm/InputFiles.cpp
  llvm/include/llvm/BinaryFormat/WasmRelocs.def
  llvm/include/llvm/MC/MCExpr.h
  llvm/lib/MC/MCExpr.cpp
  llvm/lib/MC/WasmObjectWriter.cpp
  llvm/lib/Object/WasmObjectFile.cpp
  llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
  llvm/test/CodeGen/WebAssembly/async.ll
  llvm/test/MC/WebAssembly/async.s

Index: llvm/test/MC/WebAssembly/async.s
===
--- /dev/null
+++ llvm/test/MC/WebAssembly/async.s
@@ -0,0 +1,21 @@
+# RUN: llvm-mc -triple=wasm32-unknown-unknown < %s | FileCheck %s
+# Check that it also comiled to object for format.
+# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj -o - < %s | obj2yaml | FileCheck -check-prefix=CHECK-OBJ %s
+
+foo:
+.globl foo
+.functype foo () -> ()
+end_function
+
+.section.custom_section.async,"",@
+.int32  foo@FUNCINDEX
+
+# CHECK:   .section .custom_section.async,"",@
+# CHECK-NEXT: .int32  foo@FUNCINDEX
+
+# CHECK-OBJ:- Type:CUSTOM
+# CHECK-OBJ-NEXT: Relocations:
+# CHECK-OBJ-NEXT:- Type:R_WASM_FUNCTION_INDEX_I32
+# CHECK-OBJ-NEXT:  Index:   0
+# CHECK-OBJ-NEXT:  Offset:  0x0
+# CHECK-OBJ-NEXT: Name:async
Index: llvm/test/CodeGen/WebAssembly/async.ll
===
--- /dev/null
+++ llvm/test/CodeGen/WebAssembly/async.ll
@@ -0,0 +1,17 @@
+; RUN: llc < %s -asm-verbose=false -wasm-keep-registers | FileCheck %s
+
+target triple = "wasm32-unknown-unknown"
+
+define void @test0() #0 {
+  ret void
+}
+
+define void @test1() #0 {
+  ret void
+}
+
+attributes #0 = { "wasm-async" }
+
+; CHECK:  .section.custom_section.async,"",@
+; CHECK-NEXT: .int32  test0@FUNCINDEX
+; CHECK-NEXT: .int32  test1@FUNCINDEX
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
@@ -66,6 +66,7 @@
   void emitEndOfAsmFile(Module &M) override;
   void EmitProducerInfo(Module &M);
   void EmitTargetFeatures(Module &M);
+  void EmitAsync(Module &M);
   void emitSymbolType(const MCSymbolWasm *Sym);
   void emitGlobalVariable(const GlobalVariable *GV) override;
   void emitJumpTableInfo() override;
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
@@ -438,6 +438,7 @@
 
   EmitProducerInfo(M);
   EmitTargetFeatures(M);
+  EmitAsync(M);
 }
 
 void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) {
@@ -556,6 +557,32 @@
   OutStreamer->popSection();
 }
 
+void WebAssemblyAsmPrinter::EmitAsync(Module &M) {
+  SmallVector AsyncFuncs;
+
+  for (const auto &F : M) {
+auto *Sym = cast(getSymbol(&F));
+if (F.hasFnAttribute("wasm-async"))
+  AsyncFuncs.push_back(Sym);
+  }
+  if (AsyncFuncs.size() == 0)
+return;
+
+  MCSectionWasm *AsyncSection = OutContext.getWasmSection(
+  ".custom_section.async", SectionKind::getMetadata());
+  OutStreamer->pushSection();
+  OutStreamer->switchSection(AsyncSection);
+
+  for (auto &Sym : AsyncFuncs) {
+OutStreamer->emitValue(
+MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_WASM_FUNCINDEX,
+OutContext),
+4);
+  }
+
+  OutStreamer->popSection();
+}
+
 void WebAssemblyAsmPrinter::emitConstantPool() {
   emitDecls(*MMI->getModule());
   assert(MF->getConstantPool()->getConstants().empty() &&
Index: llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
===
--- llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
+++ llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
@@ -91,6 +91,8 @@
   return wasm::R_WASM_TYPE_INDEX_LEB;
 case MCSymbolR

[PATCH] D150803: [WebAssembly] Add a new `wasm_async` clang attribute for marking async functions.

2023-06-01 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl added inline comments.



Comment at: clang/lib/CodeGen/TargetInfo.cpp:892
   if (const auto *Attr = FD->getAttr()) {
-llvm::Function *Fn = cast(GV);
+auto *Fn = cast(GV);
 llvm::AttrBuilder B(GV->getContext());

Whoops, I changed the wrong one here. I'll fix that.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150803: [WebAssembly] Add a new `wasm_async` clang attribute for marking async functions.

2023-05-31 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl updated this revision to Diff 527218.
brendandahl marked an inline comment as done.
brendandahl added a comment.

Address comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/lib/CodeGen/TargetInfo.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/CodeGen/WebAssembly/wasm-async.c
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/Sema/attr-wasm-async.c
  clang/test/Sema/wasm-async-support.c
  lld/test/wasm/async.s
  lld/test/wasm/merge-async-section.ll
  lld/wasm/InputChunks.cpp
  lld/wasm/InputFiles.cpp
  llvm/include/llvm/BinaryFormat/WasmRelocs.def
  llvm/include/llvm/MC/MCExpr.h
  llvm/lib/MC/MCExpr.cpp
  llvm/lib/MC/WasmObjectWriter.cpp
  llvm/lib/Object/WasmObjectFile.cpp
  llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
  llvm/test/CodeGen/WebAssembly/async.ll
  llvm/test/MC/WebAssembly/async.s

Index: llvm/test/MC/WebAssembly/async.s
===
--- /dev/null
+++ llvm/test/MC/WebAssembly/async.s
@@ -0,0 +1,21 @@
+# RUN: llvm-mc -triple=wasm32-unknown-unknown < %s | FileCheck %s
+# Check that it also comiled to object for format.
+# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj -o - < %s | obj2yaml | FileCheck -check-prefix=CHECK-OBJ %s
+
+foo:
+.globl foo
+.functype foo () -> ()
+end_function
+
+.section.custom_section.async,"",@
+.int32  foo@FUNCINDEX
+
+# CHECK:   .section .custom_section.async,"",@
+# CHECK-NEXT: .int32  foo@FUNCINDEX
+
+# CHECK-OBJ:- Type:CUSTOM
+# CHECK-OBJ-NEXT: Relocations:
+# CHECK-OBJ-NEXT:- Type:R_WASM_FUNCTION_INDEX_I32
+# CHECK-OBJ-NEXT:  Index:   0
+# CHECK-OBJ-NEXT:  Offset:  0x0
+# CHECK-OBJ-NEXT: Name:async
Index: llvm/test/CodeGen/WebAssembly/async.ll
===
--- /dev/null
+++ llvm/test/CodeGen/WebAssembly/async.ll
@@ -0,0 +1,17 @@
+; RUN: llc < %s -asm-verbose=false -wasm-keep-registers | FileCheck %s
+
+target triple = "wasm32-unknown-unknown"
+
+define void @test0() #0 {
+  ret void
+}
+
+define void @test1() #0 {
+  ret void
+}
+
+attributes #0 = { "wasm-async" }
+
+; CHECK:  .section.custom_section.async,"",@
+; CHECK-NEXT: .int32  test0@FUNCINDEX
+; CHECK-NEXT: .int32  test1@FUNCINDEX
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
@@ -66,6 +66,7 @@
   void emitEndOfAsmFile(Module &M) override;
   void EmitProducerInfo(Module &M);
   void EmitTargetFeatures(Module &M);
+  void EmitAsync(Module &M);
   void emitSymbolType(const MCSymbolWasm *Sym);
   void emitGlobalVariable(const GlobalVariable *GV) override;
   void emitJumpTableInfo() override;
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
@@ -438,6 +438,7 @@
 
   EmitProducerInfo(M);
   EmitTargetFeatures(M);
+  EmitAsync(M);
 }
 
 void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) {
@@ -556,6 +557,32 @@
   OutStreamer->popSection();
 }
 
+void WebAssemblyAsmPrinter::EmitAsync(Module &M) {
+  SmallVector AsyncFuncs;
+
+  for (const auto &F : M) {
+auto *Sym = cast(getSymbol(&F));
+if (F.hasFnAttribute("wasm-async"))
+  AsyncFuncs.push_back(Sym);
+  }
+  if (AsyncFuncs.size() == 0)
+return;
+
+  MCSectionWasm *AsyncSection = OutContext.getWasmSection(
+  ".custom_section.async", SectionKind::getMetadata());
+  OutStreamer->pushSection();
+  OutStreamer->switchSection(AsyncSection);
+
+  for (auto &Sym : AsyncFuncs) {
+OutStreamer->emitValue(
+MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_WASM_FUNCINDEX,
+OutContext),
+4);
+  }
+
+  OutStreamer->popSection();
+}
+
 void WebAssemblyAsmPrinter::emitConstantPool() {
   emitDecls(*MMI->getModule());
   assert(MF->getConstantPool()->getConstants().empty() &&
Index: llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
===
--- llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
+++ llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
@@ -91,6 +91,8 @@
   return wasm::R_WASM_TYPE_INDEX_LEB;
 case MCSymbolRefExpr::VK_None:
  

[PATCH] D150803: [WebAssembly] Add a new `wasm_async` clang attribute for marking async functions.

2023-05-31 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl marked 12 inline comments as done.
brendandahl added inline comments.



Comment at: clang/lib/Sema/SemaDeclAttr.cpp:7645
+  if (FD->isThisDeclarationADefinition()) {
+S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
+return;

aaron.ballman wrote:
> This diagnostic doesn't make sense to me -- how does this attribute relate to 
> ifuncs or aliases? Why should users be prohibited from writing the attribute 
> on a definition?
This was from a previous version, it's not needed anymore. I'll remove.



Comment at: llvm/test/MC/WebAssembly/async.s:10-11
+
+.section.custom_section.async,"",@
+.int32  foo@FUNCINDEX
+

brendandahl wrote:
> aheejin wrote:
> > The intention looks little weird.. Is that what `llc` emits? 
> Yeah, I wasn't really sure how to format, but that's what llc was doing. Open 
> to changing it.
Actually, it does look like something got messed up. I'll fix.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150803: [WebAssembly] Add a new `wasm_async` clang attribute for marking async functions.

2023-05-24 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl added inline comments.



Comment at: clang/include/clang/Basic/Attr.td:1984
+   TargetSpecificAttr {
+  let Spellings = [Clang<"wasm_async">];
+  let Documentation = [WebAssemblyAsyncDocs];

sbc100 wrote:
> Should we call this em_async or emscripten_async since this is an 
> emscripten-specific attribute?
I wouldn't say this is emscripten specific. You could use this with 
clang+binaryen without emscripten.



Comment at: llvm/lib/MC/MCExpr.cpp:530
+  .Case("tpoff_lo", VK_VE_TPOFF_LO32)
+  .Default(VK_Invalid);
 }

sbc100 wrote:
> Was this whole block indented?  Maybe limit this to just a single line change?
This was changed by `git-clang-format HEAD~1` . I can revert though and only 
change that line.



Comment at: llvm/test/MC/WebAssembly/async.s:10-11
+
+.section.custom_section.async,"",@
+.int32  foo@FUNCINDEX
+

aheejin wrote:
> The intention looks little weird.. Is that what `llc` emits? 
Yeah, I wasn't really sure how to format, but that's what llc was doing. Open 
to changing it.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150803: [WebAssembly] Add a new `wasm_async` clang attribute for marking async functions.

2023-05-24 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl updated this revision to Diff 525348.
brendandahl marked 5 inline comments as done.
brendandahl added a comment.

Review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150803/new/

https://reviews.llvm.org/D150803

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/lib/CodeGen/TargetInfo.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/CodeGen/WebAssembly/wasm-async.c
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  lld/test/wasm/async.ll
  lld/test/wasm/merge-async-section.ll
  lld/wasm/InputChunks.cpp
  lld/wasm/InputFiles.cpp
  llvm/include/llvm/BinaryFormat/WasmRelocs.def
  llvm/include/llvm/MC/MCExpr.h
  llvm/lib/MC/MCExpr.cpp
  llvm/lib/MC/WasmObjectWriter.cpp
  llvm/lib/Object/WasmObjectFile.cpp
  llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
  llvm/test/CodeGen/WebAssembly/async.ll
  llvm/test/MC/WebAssembly/async.s

Index: llvm/test/MC/WebAssembly/async.s
===
--- /dev/null
+++ llvm/test/MC/WebAssembly/async.s
@@ -0,0 +1,21 @@
+# RUN: llvm-mc -triple=wasm32-unknown-unknown < %s | FileCheck %s
+# Check that it also comiled to object for format.
+# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj -o - < %s | obj2yaml | FileCheck -check-prefix=CHECK-OBJ %s
+
+foo:
+.globl foo
+.functype foo () -> ()
+end_function
+
+.section.custom_section.async,"",@
+.int32  foo@FUNCINDEX
+
+# CHECK:   .section .custom_section.async,"",@
+# CHECK-NEXT: .int32  foo@FUNCINDEX
+
+# CHECK-OBJ:- Type:CUSTOM
+# CHECK-OBJ-NEXT: Relocations:
+# CHECK-OBJ-NEXT:- Type:R_WASM_FUNCTION_INDEX_I32
+# CHECK-OBJ-NEXT:Index:   0
+# CHECK-OBJ-NEXT:Offset:  0x0
+# CHECK-OBJ-NEXT: Name:async
Index: llvm/test/CodeGen/WebAssembly/async.ll
===
--- /dev/null
+++ llvm/test/CodeGen/WebAssembly/async.ll
@@ -0,0 +1,17 @@
+; RUN: llc < %s -asm-verbose=false -wasm-keep-registers | FileCheck %s
+
+target triple = "wasm32-unknown-unknown"
+
+define void @test0() #0 {
+  ret void
+}
+
+define void @test1() #0 {
+  ret void
+}
+
+attributes #0 = { "wasm-async" }
+
+; CHECK:  .section.custom_section.async,"",@
+; CHECK-NEXT: .int32  test0@FUNCINDEX
+; CHECK-NEXT: .int32  test1@FUNCINDEX
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
@@ -66,6 +66,7 @@
   void emitEndOfAsmFile(Module &M) override;
   void EmitProducerInfo(Module &M);
   void EmitTargetFeatures(Module &M);
+  void EmitAsync(Module &M);
   void emitSymbolType(const MCSymbolWasm *Sym);
   void emitGlobalVariable(const GlobalVariable *GV) override;
   void emitJumpTableInfo() override;
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
@@ -438,6 +438,7 @@
 
   EmitProducerInfo(M);
   EmitTargetFeatures(M);
+  EmitAsync(M);
 }
 
 void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) {
@@ -556,6 +557,33 @@
   OutStreamer->popSection();
 }
 
+void WebAssemblyAsmPrinter::EmitAsync(Module &M) {
+  SmallVector EmittedAsyncs;
+
+  for (const auto &F : M) {
+auto *Sym = cast(getSymbol(&F));
+if (F.hasFnAttribute("wasm-async")) {
+  EmittedAsyncs.push_back(Sym);
+}
+  }
+  if (EmittedAsyncs.size() == 0)
+return;
+
+  MCSectionWasm *AsyncSection = OutContext.getWasmSection(
+  ".custom_section.async", SectionKind::getMetadata());
+  OutStreamer->pushSection();
+  OutStreamer->switchSection(AsyncSection);
+
+  for (auto &Sym : EmittedAsyncs) {
+OutStreamer->emitValue(
+MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_WASM_FUNCINDEX,
+OutContext),
+4);
+  }
+
+  OutStreamer->popSection();
+}
+
 void WebAssemblyAsmPrinter::emitConstantPool() {
   emitDecls(*MMI->getModule());
   assert(MF->getConstantPool()->getConstants().empty() &&
Index: llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
===
--- llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
+++ llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
@@ -91,6 +91,8 @@
   return wasm::R_WASM_TYPE_INDEX_LEB;
 case MCSymbolRefExpr::VK_None:
   break;
+case MCSymbolRefExpr::VK_WASM_FUNCINDEX:
+  return wasm::R_WASM_FUNCTION_IN

[PATCH] D150803: [WebAssembly] Add a new `wasm_async` clang attribute for marking async functions.

2023-05-17 Thread Brendan Dahl via Phabricator via cfe-commits
brendandahl created this revision.
brendandahl added a reviewer: sbc100.
Herald added subscribers: pmatos, asb, jdoerfert, ecnelises, sunfish, 
hiraditya, jgravelle-google, dschuff.
Herald added a reviewer: aaron.ballman.
Herald added a project: All.
brendandahl requested review of this revision.
Herald added subscribers: llvm-commits, cfe-commits, aheejin.
Herald added projects: clang, LLVM.

The attribute may be attached to a function definition, which indicates
the function will be used with JavaScript promise integration (JSPI). The
attribute will cause the creation of a custom section named "async" that
contains each wasm_async function's index value.

A new patchable relocation type for function indexes had to be created so
the custom section could be updated during linking.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150803

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/lib/CodeGen/TargetInfo.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/CodeGen/WebAssembly/wasm-async.c
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  lld/test/wasm/async.ll
  lld/test/wasm/merge-async-section.ll
  lld/wasm/InputChunks.cpp
  lld/wasm/InputFiles.cpp
  llvm/include/llvm/BinaryFormat/WasmRelocs.def
  llvm/include/llvm/MC/MCExpr.h
  llvm/lib/MC/MCExpr.cpp
  llvm/lib/MC/WasmObjectWriter.cpp
  llvm/lib/Object/WasmObjectFile.cpp
  llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
  llvm/test/CodeGen/WebAssembly/async.ll
  llvm/test/MC/WebAssembly/async.s

Index: llvm/test/MC/WebAssembly/async.s
===
--- /dev/null
+++ llvm/test/MC/WebAssembly/async.s
@@ -0,0 +1,21 @@
+# RUN: llvm-mc -triple=wasm32-unknown-unknown < %s | FileCheck %s
+# Check that it also comiled to object for format.
+# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj -o - < %s | obj2yaml | FileCheck -check-prefix=CHECK-OBJ %s
+
+foo:
+.globl foo
+.functype foo () -> ()
+end_function
+
+.section.custom_section.async,"",@
+.int32  foo@FUNCINDEX
+
+# CHECK:   .section .custom_section.async,"",@
+# CHECK-NEXT: .int32  foo@FUNCINDEX
+
+# CHECK-OBJ:- Type:CUSTOM
+# CHECK-OBJ-NEXT: Relocations:
+# CHECK-OBJ-NEXT:- Type:R_WASM_FUNCTION_INDEX_I32
+# CHECK-OBJ-NEXT:Index:   0
+# CHECK-OBJ-NEXT:Offset:  0x0
+# CHECK-OBJ-NEXT: Name:async
Index: llvm/test/CodeGen/WebAssembly/async.ll
===
--- /dev/null
+++ llvm/test/CodeGen/WebAssembly/async.ll
@@ -0,0 +1,18 @@
+; RUN: llc < %s -asm-verbose=false -wasm-keep-registers | FileCheck %s
+
+target triple = "wasm32-unknown-unknown"
+
+define void @test0() #0 {
+  ret void
+}
+
+define void @test1() #1 {
+  ret void
+}
+
+attributes #0 = { "wasm-async" }
+attributes #1 = { "wasm-async" }
+
+; CHECK:  .section.custom_section.async,"",@
+; CHECK-NEXT: .int32  test0@FUNCINDEX
+; CHECK-NEXT: .int32  test1@FUNCINDEX
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h
@@ -66,6 +66,7 @@
   void emitEndOfAsmFile(Module &M) override;
   void EmitProducerInfo(Module &M);
   void EmitTargetFeatures(Module &M);
+  void EmitAsync(Module &M);
   void emitSymbolType(const MCSymbolWasm *Sym);
   void emitGlobalVariable(const GlobalVariable *GV) override;
   void emitJumpTableInfo() override;
Index: llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
===
--- llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
@@ -438,6 +438,7 @@
 
   EmitProducerInfo(M);
   EmitTargetFeatures(M);
+  EmitAsync(M);
 }
 
 void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) {
@@ -556,6 +557,33 @@
   OutStreamer->popSection();
 }
 
+void WebAssemblyAsmPrinter::EmitAsync(Module &M) {
+  SmallVector EmittedAsyncs;
+
+  for (const auto &F : M) {
+auto *Sym = cast(getSymbol(&F));
+if (F.hasFnAttribute("wasm-async")) {
+  EmittedAsyncs.push_back(Sym);
+}
+  }
+  if (EmittedAsyncs.size() == 0)
+return;
+
+  MCSectionWasm *AsyncSection = OutContext.getWasmSection(
+  ".custom_section.async", SectionKind::getMetadata());
+  OutStreamer->pushSection();
+  OutStreamer->switchSection(AsyncSection);
+
+  for (auto &Sym : EmittedAsyncs) {
+OutStreamer->emitValue(
+MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_WASM_FUNCINDEX,
+OutContext),
+4);
+  }
+
+  OutStreamer->popSection();
+}
+
 void WebAssemblyAsmPri