[clang] [llvm] IR: Add module level attribution language-standard (PR #93159)

2024-05-23 Thread YunQiang Su via cfe-commits

https://github.com/wzssyqa closed 
https://github.com/llvm/llvm-project/pull/93159
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] IR: Add module level attribution language-standard (PR #93159)

2024-05-23 Thread YunQiang Su via cfe-commits

wzssyqa wrote:

Let's close it.

https://github.com/llvm/llvm-project/pull/93159
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] IR: Add module level attribution language-standard (PR #93159)

2024-05-23 Thread Matt Arsenault via cfe-commits

https://github.com/arsenm requested changes to this pull request.

You cannot encode language standards in this. We should simply have different 
operations that provide the range of semantics and not make the IR modal 

https://github.com/llvm/llvm-project/pull/93159
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] IR: Add module level attribution language-standard (PR #93159)

2024-05-23 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-codegen

Author: YunQiang Su (wzssyqa)


Changes

The backend may have interest on the language-standard level of source file. 
Let's pass it to IR.

In a ll file, it looks like
   language-standard = "gnu17"

---
Full diff: https://github.com/llvm/llvm-project/pull/93159.diff


7 Files Affected:

- (modified) clang/lib/CodeGen/ModuleBuilder.cpp (+3) 
- (modified) llvm/include/llvm/Bitcode/LLVMBitCodes.h (+3) 
- (modified) llvm/include/llvm/IR/Module.h (+7) 
- (modified) llvm/lib/Bitcode/Reader/BitcodeReader.cpp (+21-1) 
- (modified) llvm/lib/Bitcode/Writer/BitcodeWriter.cpp (+23) 
- (modified) llvm/lib/IR/AsmWriter.cpp (+6) 
- (modified) llvm/lib/IR/Module.cpp (+2-2) 


``diff
diff --git a/clang/lib/CodeGen/ModuleBuilder.cpp 
b/clang/lib/CodeGen/ModuleBuilder.cpp
index df85295cfb2e2..cf07b09b93fd7 100644
--- a/clang/lib/CodeGen/ModuleBuilder.cpp
+++ b/clang/lib/CodeGen/ModuleBuilder.cpp
@@ -153,6 +153,9 @@ namespace {
 
   M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
   M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
+  M->setLanguageStandard(
+  LangStandard::getLangStandardForKind(Ctx->getLangOpts().LangStd)
+  .getName());
   const auto  = Ctx->getTargetInfo().getSDKVersion();
   if (!SDKVersion.empty())
 M->setSDKVersion(SDKVersion);
diff --git a/llvm/include/llvm/Bitcode/LLVMBitCodes.h 
b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
index 909eb833c601a..cba77c322fdb4 100644
--- a/llvm/include/llvm/Bitcode/LLVMBitCodes.h
+++ b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
@@ -120,6 +120,9 @@ enum ModuleCodes {
 
   // IFUNC: [ifunc value type, addrspace, resolver val#, linkage, visibility]
   MODULE_CODE_IFUNC = 18,
+
+  // LANGUAGE_STANDARD: [strchr x N]
+  MODULE_CODE_LANGUAGE_STANDARD = 19,
 };
 
 /// PARAMATTR blocks have code for defining a parameter attribute set.
diff --git a/llvm/include/llvm/IR/Module.h b/llvm/include/llvm/IR/Module.h
index 6135e15fd030f..300dc5c5630dc 100644
--- a/llvm/include/llvm/IR/Module.h
+++ b/llvm/include/llvm/IR/Module.h
@@ -196,6 +196,7 @@ class LLVM_EXTERNAL_VISIBILITY Module {
   ///< recorded in bitcode.
   std::string TargetTriple;   ///< Platform target triple Module compiled 
on
   ///< Format: (arch)(sub)-(vendor)-(sys0-(abi)
+  std::string LanguageStandard;   ///< Language standard: c89/c99/c11/c23 etc.
   NamedMDSymTabType NamedMDSymTab;  ///< NamedMDNode names.
   DataLayout DL;  ///< DataLayout associated with the module
   StringMap
@@ -277,6 +278,9 @@ class LLVM_EXTERNAL_VISIBILITY Module {
   /// contain the source file name.
   const std::string () const { return SourceFileName; }
 
+  /// Get the module's language standard.
+  const std::string () const { return LanguageStandard; }
+
   /// Get a short "name" for the module.
   ///
   /// This is useful for debugging or logging. It is essentially a convenience
@@ -339,6 +343,9 @@ class LLVM_EXTERNAL_VISIBILITY Module {
   /// Set the target triple.
   void setTargetTriple(StringRef T) { TargetTriple = std::string(T); }
 
+  /// Set the language standard.
+  void setLanguageStandard(StringRef S) { LanguageStandard = std::string(S); }
+
   /// Set the module-scope inline assembly blocks.
   /// A trailing newline is added if the input doesn't have one.
   void setModuleInlineAsm(StringRef Asm) {
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp 
b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index c9295344f8080..8078291477886 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -946,6 +946,9 @@ class ModuleSummaryIndexBitcodeReader : public 
BitcodeReaderBase {
   /// Original source file name recorded in a bitcode record.
   std::string SourceFileName;
 
+  /// Original language standard recorded in a bitcode record.
+  std::string LanguageStandard;
+
   /// The string identifier given to this module by the client, normally the
   /// path to the bitcode file.
   StringRef ModulePath;
@@ -4613,13 +4616,22 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit,
   VSTOffset = Record[0] - 1;
   break;
 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
-case bitc::MODULE_CODE_SOURCE_FILENAME:
+case bitc::MODULE_CODE_SOURCE_FILENAME: {
   SmallString<128> ValueName;
   if (convertToString(Record, 0, ValueName))
 return error("Invalid record");
   TheModule->setSourceFileName(ValueName);
   break;
 }
+/// MODULE_CODE_LANGUAGE_STANDARD: [strchar x N]
+case bitc::MODULE_CODE_LANGUAGE_STANDARD: {
+  SmallString<128> ValueLangStd;
+  if (convertToString(Record, 0, ValueLangStd))
+return error("Invalid record");
+  TheModule->setLanguageStandard(ValueLangStd);
+  break;
+}
+}
 Record.clear();
   }
   this->ValueTypeCallback = std::nullopt;
@@ -7227,6 +7239,14 

[clang] [llvm] IR: Add module level attribution language-standard (PR #93159)

2024-05-23 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-llvm-ir

Author: YunQiang Su (wzssyqa)


Changes

The backend may have interest on the language-standard level of source file. 
Let's pass it to IR.

In a ll file, it looks like
   language-standard = "gnu17"

---
Full diff: https://github.com/llvm/llvm-project/pull/93159.diff


7 Files Affected:

- (modified) clang/lib/CodeGen/ModuleBuilder.cpp (+3) 
- (modified) llvm/include/llvm/Bitcode/LLVMBitCodes.h (+3) 
- (modified) llvm/include/llvm/IR/Module.h (+7) 
- (modified) llvm/lib/Bitcode/Reader/BitcodeReader.cpp (+21-1) 
- (modified) llvm/lib/Bitcode/Writer/BitcodeWriter.cpp (+23) 
- (modified) llvm/lib/IR/AsmWriter.cpp (+6) 
- (modified) llvm/lib/IR/Module.cpp (+2-2) 


``diff
diff --git a/clang/lib/CodeGen/ModuleBuilder.cpp 
b/clang/lib/CodeGen/ModuleBuilder.cpp
index df85295cfb2e2..cf07b09b93fd7 100644
--- a/clang/lib/CodeGen/ModuleBuilder.cpp
+++ b/clang/lib/CodeGen/ModuleBuilder.cpp
@@ -153,6 +153,9 @@ namespace {
 
   M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
   M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
+  M->setLanguageStandard(
+  LangStandard::getLangStandardForKind(Ctx->getLangOpts().LangStd)
+  .getName());
   const auto  = Ctx->getTargetInfo().getSDKVersion();
   if (!SDKVersion.empty())
 M->setSDKVersion(SDKVersion);
diff --git a/llvm/include/llvm/Bitcode/LLVMBitCodes.h 
b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
index 909eb833c601a..cba77c322fdb4 100644
--- a/llvm/include/llvm/Bitcode/LLVMBitCodes.h
+++ b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
@@ -120,6 +120,9 @@ enum ModuleCodes {
 
   // IFUNC: [ifunc value type, addrspace, resolver val#, linkage, visibility]
   MODULE_CODE_IFUNC = 18,
+
+  // LANGUAGE_STANDARD: [strchr x N]
+  MODULE_CODE_LANGUAGE_STANDARD = 19,
 };
 
 /// PARAMATTR blocks have code for defining a parameter attribute set.
diff --git a/llvm/include/llvm/IR/Module.h b/llvm/include/llvm/IR/Module.h
index 6135e15fd030f..300dc5c5630dc 100644
--- a/llvm/include/llvm/IR/Module.h
+++ b/llvm/include/llvm/IR/Module.h
@@ -196,6 +196,7 @@ class LLVM_EXTERNAL_VISIBILITY Module {
   ///< recorded in bitcode.
   std::string TargetTriple;   ///< Platform target triple Module compiled 
on
   ///< Format: (arch)(sub)-(vendor)-(sys0-(abi)
+  std::string LanguageStandard;   ///< Language standard: c89/c99/c11/c23 etc.
   NamedMDSymTabType NamedMDSymTab;  ///< NamedMDNode names.
   DataLayout DL;  ///< DataLayout associated with the module
   StringMap
@@ -277,6 +278,9 @@ class LLVM_EXTERNAL_VISIBILITY Module {
   /// contain the source file name.
   const std::string () const { return SourceFileName; }
 
+  /// Get the module's language standard.
+  const std::string () const { return LanguageStandard; }
+
   /// Get a short "name" for the module.
   ///
   /// This is useful for debugging or logging. It is essentially a convenience
@@ -339,6 +343,9 @@ class LLVM_EXTERNAL_VISIBILITY Module {
   /// Set the target triple.
   void setTargetTriple(StringRef T) { TargetTriple = std::string(T); }
 
+  /// Set the language standard.
+  void setLanguageStandard(StringRef S) { LanguageStandard = std::string(S); }
+
   /// Set the module-scope inline assembly blocks.
   /// A trailing newline is added if the input doesn't have one.
   void setModuleInlineAsm(StringRef Asm) {
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp 
b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index c9295344f8080..8078291477886 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -946,6 +946,9 @@ class ModuleSummaryIndexBitcodeReader : public 
BitcodeReaderBase {
   /// Original source file name recorded in a bitcode record.
   std::string SourceFileName;
 
+  /// Original language standard recorded in a bitcode record.
+  std::string LanguageStandard;
+
   /// The string identifier given to this module by the client, normally the
   /// path to the bitcode file.
   StringRef ModulePath;
@@ -4613,13 +4616,22 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit,
   VSTOffset = Record[0] - 1;
   break;
 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
-case bitc::MODULE_CODE_SOURCE_FILENAME:
+case bitc::MODULE_CODE_SOURCE_FILENAME: {
   SmallString<128> ValueName;
   if (convertToString(Record, 0, ValueName))
 return error("Invalid record");
   TheModule->setSourceFileName(ValueName);
   break;
 }
+/// MODULE_CODE_LANGUAGE_STANDARD: [strchar x N]
+case bitc::MODULE_CODE_LANGUAGE_STANDARD: {
+  SmallString<128> ValueLangStd;
+  if (convertToString(Record, 0, ValueLangStd))
+return error("Invalid record");
+  TheModule->setLanguageStandard(ValueLangStd);
+  break;
+}
+}
 Record.clear();
   }
   this->ValueTypeCallback = std::nullopt;
@@ -7227,6 +7239,14 @@ 

[clang] [llvm] IR: Add module level attribution language-standard (PR #93159)

2024-05-23 Thread YunQiang Su via cfe-commits

https://github.com/wzssyqa converted_to_draft 
https://github.com/llvm/llvm-project/pull/93159
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] IR: Add module level attribution language-standard (PR #93159)

2024-05-23 Thread YunQiang Su via cfe-commits

https://github.com/wzssyqa created 
https://github.com/llvm/llvm-project/pull/93159

The backend may have interest on the language-standard level of source file. 
Let's pass it to IR.

In a ll file, it looks like
   language-standard = "gnu17"

>From 3e413bae6ba18276e4d47075d48d656e4f732b5e Mon Sep 17 00:00:00 2001
From: YunQiang Su 
Date: Thu, 23 May 2024 17:33:34 +0800
Subject: [PATCH] IR: Add module level attribution language-standard

The backend may have interest on the language-standard level
of source file. Let's pass it to IR.

In a ll file, it looks like
   language-standard = "gnu17"
---
 clang/lib/CodeGen/ModuleBuilder.cpp   |  3 +++
 llvm/include/llvm/Bitcode/LLVMBitCodes.h  |  3 +++
 llvm/include/llvm/IR/Module.h |  7 +++
 llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 22 +-
 llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 23 +++
 llvm/lib/IR/AsmWriter.cpp |  6 ++
 llvm/lib/IR/Module.cpp|  4 ++--
 7 files changed, 65 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/ModuleBuilder.cpp 
b/clang/lib/CodeGen/ModuleBuilder.cpp
index df85295cfb2e2..cf07b09b93fd7 100644
--- a/clang/lib/CodeGen/ModuleBuilder.cpp
+++ b/clang/lib/CodeGen/ModuleBuilder.cpp
@@ -153,6 +153,9 @@ namespace {
 
   M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
   M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
+  M->setLanguageStandard(
+  LangStandard::getLangStandardForKind(Ctx->getLangOpts().LangStd)
+  .getName());
   const auto  = Ctx->getTargetInfo().getSDKVersion();
   if (!SDKVersion.empty())
 M->setSDKVersion(SDKVersion);
diff --git a/llvm/include/llvm/Bitcode/LLVMBitCodes.h 
b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
index 909eb833c601a..cba77c322fdb4 100644
--- a/llvm/include/llvm/Bitcode/LLVMBitCodes.h
+++ b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
@@ -120,6 +120,9 @@ enum ModuleCodes {
 
   // IFUNC: [ifunc value type, addrspace, resolver val#, linkage, visibility]
   MODULE_CODE_IFUNC = 18,
+
+  // LANGUAGE_STANDARD: [strchr x N]
+  MODULE_CODE_LANGUAGE_STANDARD = 19,
 };
 
 /// PARAMATTR blocks have code for defining a parameter attribute set.
diff --git a/llvm/include/llvm/IR/Module.h b/llvm/include/llvm/IR/Module.h
index 6135e15fd030f..300dc5c5630dc 100644
--- a/llvm/include/llvm/IR/Module.h
+++ b/llvm/include/llvm/IR/Module.h
@@ -196,6 +196,7 @@ class LLVM_EXTERNAL_VISIBILITY Module {
   ///< recorded in bitcode.
   std::string TargetTriple;   ///< Platform target triple Module compiled 
on
   ///< Format: (arch)(sub)-(vendor)-(sys0-(abi)
+  std::string LanguageStandard;   ///< Language standard: c89/c99/c11/c23 etc.
   NamedMDSymTabType NamedMDSymTab;  ///< NamedMDNode names.
   DataLayout DL;  ///< DataLayout associated with the module
   StringMap
@@ -277,6 +278,9 @@ class LLVM_EXTERNAL_VISIBILITY Module {
   /// contain the source file name.
   const std::string () const { return SourceFileName; }
 
+  /// Get the module's language standard.
+  const std::string () const { return LanguageStandard; }
+
   /// Get a short "name" for the module.
   ///
   /// This is useful for debugging or logging. It is essentially a convenience
@@ -339,6 +343,9 @@ class LLVM_EXTERNAL_VISIBILITY Module {
   /// Set the target triple.
   void setTargetTriple(StringRef T) { TargetTriple = std::string(T); }
 
+  /// Set the language standard.
+  void setLanguageStandard(StringRef S) { LanguageStandard = std::string(S); }
+
   /// Set the module-scope inline assembly blocks.
   /// A trailing newline is added if the input doesn't have one.
   void setModuleInlineAsm(StringRef Asm) {
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp 
b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index c9295344f8080..8078291477886 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -946,6 +946,9 @@ class ModuleSummaryIndexBitcodeReader : public 
BitcodeReaderBase {
   /// Original source file name recorded in a bitcode record.
   std::string SourceFileName;
 
+  /// Original language standard recorded in a bitcode record.
+  std::string LanguageStandard;
+
   /// The string identifier given to this module by the client, normally the
   /// path to the bitcode file.
   StringRef ModulePath;
@@ -4613,13 +4616,22 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit,
   VSTOffset = Record[0] - 1;
   break;
 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
-case bitc::MODULE_CODE_SOURCE_FILENAME:
+case bitc::MODULE_CODE_SOURCE_FILENAME: {
   SmallString<128> ValueName;
   if (convertToString(Record, 0, ValueName))
 return error("Invalid record");
   TheModule->setSourceFileName(ValueName);
   break;
 }
+/// MODULE_CODE_LANGUAGE_STANDARD: [strchar x N]
+case