[clang] 4f173c0 - [clang][AVR] Support variable decorator '__flash'

Ben Shi via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 9 20:24:07 PDT 2021


Author: Ben Shi
Date: 2021-04-10T11:23:55+08:00
New Revision: 4f173c0c42d02b14ab388a826ef0d463a07d7953

URL: https://github.com/llvm/llvm-project/commit/4f173c0c42d02b14ab388a826ef0d463a07d7953
DIFF: https://github.com/llvm/llvm-project/commit/4f173c0c42d02b14ab388a826ef0d463a07d7953.diff

LOG: [clang][AVR] Support variable decorator '__flash'

Reviewed By: Anastasia

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

Added: 
    clang/test/CodeGen/avr-flash.c

Modified: 
    clang/include/clang/Basic/DiagnosticFrontendKinds.td
    clang/lib/Basic/Targets/AVR.cpp
    clang/lib/CodeGen/TargetInfo.cpp
    clang/test/CodeGen/address-space-avr.c

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 3086e922d9ed6..39fdefc77fbee 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -158,6 +158,8 @@ def err_verify_invalid_no_diags : Error<
     "%select{'expected-no-diagnostics' directive|other expected directives}0">;
 def err_verify_no_directives : Error<
     "no expected directives found: consider use of 'expected-no-diagnostics'">;
+def err_verify_nonconst_addrspace : Error<
+  "qualifier 'const' is needed for variables in address space '%0'">;
 
 def note_fixit_applied : Note<"FIX-IT applied suggested code changes">;
 def note_fixit_in_macro : Note<

diff  --git a/clang/lib/Basic/Targets/AVR.cpp b/clang/lib/Basic/Targets/AVR.cpp
index 664eea0de8416..e87b7338c4d6e 100644
--- a/clang/lib/Basic/Targets/AVR.cpp
+++ b/clang/lib/Basic/Targets/AVR.cpp
@@ -308,6 +308,7 @@ void AVRTargetInfo::getTargetDefines(const LangOptions &Opts,
   Builder.defineMacro("__AVR");
   Builder.defineMacro("__AVR__");
   Builder.defineMacro("__ELF__");
+  Builder.defineMacro("__flash", "__attribute__((address_space(1)))");
 
   if (!this->CPU.empty()) {
     auto It = llvm::find_if(

diff  --git a/clang/lib/CodeGen/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp
index de3d3f86fe9b9..7f4deb21d6ed0 100644
--- a/clang/lib/CodeGen/TargetInfo.cpp
+++ b/clang/lib/CodeGen/TargetInfo.cpp
@@ -8108,6 +8108,19 @@ class AVRTargetCodeGenInfo : public TargetCodeGenInfo {
   AVRTargetCodeGenInfo(CodeGenTypes &CGT)
       : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
 
+  LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
+                                  const VarDecl *D) const override {
+    // Check if a global/static variable is defined within address space 1
+    // but not constant.
+    LangAS AS = D->getType().getAddressSpace();
+    if (isTargetAddressSpace(AS) && toTargetAddressSpace(AS) == 1 &&
+        !D->getType().isConstQualified())
+      CGM.getDiags().Report(D->getLocation(),
+                            diag::err_verify_nonconst_addrspace)
+          << "__flash";
+    return TargetCodeGenInfo::getGlobalVarAddressSpace(CGM, D);
+  }
+
   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
                            CodeGen::CodeGenModule &CGM) const override {
     if (GV->isDeclaration())

diff  --git a/clang/test/CodeGen/address-space-avr.c b/clang/test/CodeGen/address-space-avr.c
index 6c32dff2d0fe3..ea78c895f63dc 100644
--- a/clang/test/CodeGen/address-space-avr.c
+++ b/clang/test/CodeGen/address-space-avr.c
@@ -1,12 +1,21 @@
 // RUN: %clang_cc1 -triple avr -emit-llvm < %s | FileCheck %s
 
-// Test that function declarations in nonzero address spaces without prototype
-// are called correctly.
+// CHECK: @var0 {{.*}} addrspace(1) constant [3 x i16]
+// CHECK: @bar.var2 {{.*}} addrspace(1) constant [3 x i16]
+// CHECK: @var1 {{.*}} addrspace(1) constant [3 x i16]
 
 // CHECK: define{{.*}} void @bar() addrspace(1)
-// CHECK: call addrspace(1) void bitcast (void (...) addrspace(1)* @foo to void (i16) addrspace(1)*)(i16 3)
+// CHECK: call addrspace(1) void bitcast (void (...) addrspace(1)* @foo to void (i16) addrspace(1)*)
 // CHECK: declare void @foo(...) addrspace(1)
+
+__flash const int var0[] = {999, 888, 777};
+__flash static const int var1[] = {111, 222, 333};
+
+int i;
+
 void foo();
-void bar(void) {
-	foo(3);
+
+void bar() {
+  static __flash const int var2[] = {555, 666, 777};
+  foo(var1[i]);
 }

diff  --git a/clang/test/CodeGen/avr-flash.c b/clang/test/CodeGen/avr-flash.c
new file mode 100644
index 0000000000000..99648137d0f18
--- /dev/null
+++ b/clang/test/CodeGen/avr-flash.c
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -triple avr -emit-llvm-only -verify %s
+
+int foo(void) {
+  static __flash int b[] = {4, 6}; // expected-error {{qualifier 'const' is needed for variables in address space '__flash'}}
+  return b[0];
+}


        


More information about the cfe-commits mailing list