Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[IR] Don't allow label arguments #137799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2025
Merged

[IR] Don't allow label arguments #137799

merged 2 commits into from
Apr 30, 2025

Conversation

nikic
Copy link
Contributor

@nikic nikic commented Apr 29, 2025

We currently accept label arguments to inline asm calls. This support predates both blockaddresses and callbr and is only covered by one X86 test. Remove it in favor of callbr (or at least blockaddress, though that cannot guarantee correct codegen, just like using block labels directly can't).

I didn't bother implementing bitcode upgrade support for this, but I can add it if desired.

We currently accept label arguments to inline asm calls. Remove
this in favor of callbr, or at least the use of blockaddress.

I didn't bother implementing bitcode upgrade support for this,
as we just have one very old test for this functionality, but I
can add it if desired.
@llvmbot
Copy link
Member

llvmbot commented Apr 29, 2025

@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-llvm-ir

Author: Nikita Popov (nikic)

Changes

We currently accept label arguments to inline asm calls. This support predates both blockaddresses and callbr and is only covered by one X86 test. Remove it in favor of callbr (or at least blockaddress, though that cannot guarantee correct codegen, just like using block labels directory can't).

I didn't bother implementing bitcode upgrade support for this, but I can add it if desired.


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

8 Files Affected:

  • (modified) llvm/lib/AsmParser/LLParser.cpp (+3-1)
  • (modified) llvm/lib/Bitcode/Writer/BitcodeWriter.cpp (+2-7)
  • (modified) llvm/lib/IR/Type.cpp (+1-1)
  • (modified) llvm/lib/IR/Verifier.cpp (-2)
  • (added) llvm/test/Assembler/invalid-label-call-arg.ll (+9)
  • (modified) llvm/test/Assembler/invalid-label.ll (+1-1)
  • (modified) llvm/test/CodeGen/X86/asm-block-labels.ll (+3-3)
  • (modified) llvm/test/Verifier/invalid-label-param.ll (+1-5)
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 370d124dc42b4..dd37d9b9e3796 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -3072,6 +3072,8 @@ bool LLParser::parseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
     Value *V;
     if (parseType(ArgTy, ArgLoc))
       return true;
+    if (!FunctionType::isValidArgumentType(ArgTy))
+      return error(ArgLoc, "invalid type for function argument");
 
     AttrBuilder ArgAttrs(M->getContext());
 
@@ -3381,7 +3383,7 @@ bool LLParser::parseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
         CurValID = ArgID + 1;
       }
 
-      if (!ArgTy->isFirstClassType())
+      if (!FunctionType::isValidArgumentType(ArgTy))
         return error(TypeLoc, "invalid type for function argument");
 
       ArgList.emplace_back(TypeLoc, ArgTy,
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index 73bed85c65b3d..5226db9db1e03 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -3488,13 +3488,8 @@ void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
     pushValueAndType(CI.getCalledOperand(), InstID, Vals); // Callee
 
     // Emit value #'s for the fixed parameters.
-    for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
-      // Check for labels (can happen with asm labels).
-      if (FTy->getParamType(i)->isLabelTy())
-        Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
-      else
-        pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
-    }
+    for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
+      pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
 
     // Emit type/value pairs for varargs params.
     if (FTy->isVarArg()) {
diff --git a/llvm/lib/IR/Type.cpp b/llvm/lib/IR/Type.cpp
index 884609e734298..1c68b173364f9 100644
--- a/llvm/lib/IR/Type.cpp
+++ b/llvm/lib/IR/Type.cpp
@@ -390,7 +390,7 @@ bool FunctionType::isValidReturnType(Type *RetTy) {
 }
 
 bool FunctionType::isValidArgumentType(Type *ArgTy) {
-  return ArgTy->isFirstClassType();
+  return ArgTy->isFirstClassType() && !ArgTy->isLabelTy();
 }
 
 //===----------------------------------------------------------------------===//
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 262e7022099d7..28ba944d8ede3 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -2934,8 +2934,6 @@ void Verifier::visitFunction(const Function &F) {
           FT->getParamType(i));
     Check(Arg.getType()->isFirstClassType(),
           "Function arguments must have first-class types!", &Arg);
-    Check(!Arg.getType()->isLabelTy(),
-          "Function argument cannot be of label type!", &Arg, &F);
     if (!IsIntrinsic) {
       Check(!Arg.getType()->isMetadataTy(),
             "Function takes metadata but isn't an intrinsic", &Arg, &F);
diff --git a/llvm/test/Assembler/invalid-label-call-arg.ll b/llvm/test/Assembler/invalid-label-call-arg.ll
new file mode 100644
index 0000000000000..575ec6b05e38e
--- /dev/null
+++ b/llvm/test/Assembler/invalid-label-call-arg.ll
@@ -0,0 +1,9 @@
+; RUN: not llvm-as < %s 2>&1 | FileCheck %s
+
+; CHECK: invalid type for function argument
+define void @test() {
+bb:
+  call void asm "", ""(label %bb)
+  ret void
+}
+
diff --git a/llvm/test/Assembler/invalid-label.ll b/llvm/test/Assembler/invalid-label.ll
index 33dc63610aae3..ae464b362135b 100644
--- a/llvm/test/Assembler/invalid-label.ll
+++ b/llvm/test/Assembler/invalid-label.ll
@@ -2,7 +2,7 @@
 ; RUN: FileCheck %s < %t
 ; Test the case where an invalid label name is used
 
-; CHECK: unable to create block named 'bb'
+; CHECK: invalid type for function argument
 
 define void @test(label %bb) {
 bb:
diff --git a/llvm/test/CodeGen/X86/asm-block-labels.ll b/llvm/test/CodeGen/X86/asm-block-labels.ll
index 44bb7518ab6ee..fd78c793fa73e 100644
--- a/llvm/test/CodeGen/X86/asm-block-labels.ll
+++ b/llvm/test/CodeGen/X86/asm-block-labels.ll
@@ -13,7 +13,7 @@ entry:
 	call void asm sideeffect "int $$1", "~{dirflag},~{fpsr},~{flags},~{memory}"( )
 	call void asm sideeffect ".file \22block12.c\22", "~{dirflag},~{fpsr},~{flags}"( )
 	call void asm sideeffect ".line 2", "~{dirflag},~{fpsr},~{flags}"( )
-	call void asm sideeffect "brl ${0:l}", "X,~{dirflag},~{fpsr},~{flags},~{memory}"( label %"LASM$foo" )
+	call void asm sideeffect "brl ${0:l}", "X,~{dirflag},~{fpsr},~{flags},~{memory}"(ptr blockaddress(@bar, %"LASM$foo"))
 	br label %return
 
 return:		; preds = %"LASM$foo"
@@ -24,7 +24,7 @@ define void @baz() {
 entry:
 	call void asm sideeffect ".file \22block12.c\22", "~{dirflag},~{fpsr},~{flags}"( )
 	call void asm sideeffect ".line 3", "~{dirflag},~{fpsr},~{flags}"( )
-	call void asm sideeffect "brl ${0:l}", "X,~{dirflag},~{fpsr},~{flags},~{memory}"( label %"LASM$foo" )
+	call void asm sideeffect "brl ${0:l}", "X,~{dirflag},~{fpsr},~{flags},~{memory}"(ptr blockaddress(@baz, %"LASM$foo"))
 	call void asm sideeffect ".file \22block12.c\22", "~{dirflag},~{fpsr},~{flags}"( )
 	call void asm sideeffect ".line 4", "~{dirflag},~{fpsr},~{flags}"( )
 	call void asm sideeffect "int $$1", "~{dirflag},~{fpsr},~{flags},~{memory}"( )
@@ -42,7 +42,7 @@ return:		; preds = %"LASM$foo"
 
 define void @quux() {
 entry:
-	call void asm sideeffect inteldialect "brl ${0:l}", "X,~{dirflag},~{fpsr},~{flags},~{memory}"( label %"LASM$foo" )
+	call void asm sideeffect inteldialect "brl ${0:l}", "X,~{dirflag},~{fpsr},~{flags},~{memory}"(ptr blockaddress(@quux, %"LASM$foo"))
 	br label %"LASM$foo"
 
 "LASM$foo":		; preds = %entry
diff --git a/llvm/test/Verifier/invalid-label-param.ll b/llvm/test/Verifier/invalid-label-param.ll
index c89ce99ce1e9f..92d8b80086dcb 100644
--- a/llvm/test/Verifier/invalid-label-param.ll
+++ b/llvm/test/Verifier/invalid-label-param.ll
@@ -7,8 +7,4 @@ define void @invalid_arg_type(i32 %0) {
 }
 
 declare void @foo(label)
-; CHECK: Function argument cannot be of label type!
-; CHECK-NEXT: label %0
-; CHECK-NEXT: ptr @foo
-
-
+; CHECK: invalid type for function argument

Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add to release notes?

Copy link
Contributor

@phoebewang phoebewang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@nikic nikic merged commit 38cb7d5 into llvm:main Apr 30, 2025
12 checks passed
@nikic nikic deleted the no-label-args branch April 30, 2025 07:11
nikic added a commit to nikic/llvm-project that referenced this pull request Apr 30, 2025
After llvm#137799 and
llvm#137816, instruction uses
of BasicBlocks are always terminators, so assert that instead of
checking it.
nikic added a commit that referenced this pull request Apr 30, 2025
After #137799 and
#137816, instruction uses of
BasicBlocks are always terminators, so assert that instead of checking
it.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
We currently accept label arguments to inline asm calls. This support
predates both blockaddresses and callbr and is only covered by one X86
test. Remove it in favor of callbr (or at least blockaddress, though
that cannot guarantee correct codegen, just like using block labels
directly can't).

I didn't bother implementing bitcode upgrade support for this, but I can
add it if desired.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
After llvm#137799 and
llvm#137816, instruction uses of
BasicBlocks are always terminators, so assert that instead of checking
it.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
We currently accept label arguments to inline asm calls. This support
predates both blockaddresses and callbr and is only covered by one X86
test. Remove it in favor of callbr (or at least blockaddress, though
that cannot guarantee correct codegen, just like using block labels
directly can't).

I didn't bother implementing bitcode upgrade support for this, but I can
add it if desired.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
After llvm#137799 and
llvm#137816, instruction uses of
BasicBlocks are always terminators, so assert that instead of checking
it.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
We currently accept label arguments to inline asm calls. This support
predates both blockaddresses and callbr and is only covered by one X86
test. Remove it in favor of callbr (or at least blockaddress, though
that cannot guarantee correct codegen, just like using block labels
directly can't).

I didn't bother implementing bitcode upgrade support for this, but I can
add it if desired.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
After llvm#137799 and
llvm#137816, instruction uses of
BasicBlocks are always terminators, so assert that instead of checking
it.
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request May 6, 2025
…7931)

After llvm/llvm-project#137799 and
llvm/llvm-project#137816, instruction uses of
BasicBlocks are always terminators, so assert that instead of checking
it.
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
We currently accept label arguments to inline asm calls. This support
predates both blockaddresses and callbr and is only covered by one X86
test. Remove it in favor of callbr (or at least blockaddress, though
that cannot guarantee correct codegen, just like using block labels
directly can't).

I didn't bother implementing bitcode upgrade support for this, but I can
add it if desired.
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
After llvm#137799 and
llvm#137816, instruction uses of
BasicBlocks are always terminators, so assert that instead of checking
it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants