Skip to contentSkip to conversation

sparse, llvm: Fix 'void' return type code generation

PATCH In review Pekka Enberg 2 participants 2 messages Aug 19, 2012 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sparse, llvm: Fix 'void' return type code generation

Jonathan Neuschäfer reports:

A simple function like this will compile to the following llvm bitcode:

        /* C */
        void func(void) {
                return;
        }

        /* LLVM */
        define i8 @func() {
        L0:
          ret void
        }

The return type of the function and the type in the return instruction don't match.

I found this inconsistency by running LLVM's bitcode validation on the bitcode produced by sparse-llvm.

Move 'void *' special-casing from sym_basetype_type() to sym_ptr_type() to fix the issue.

Reported by: Jonathan Neuschäfer <[email protected]>

Cc Christopher Li <[email protected]>
Cc Jeff Garzik <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 sparse-llvm.c                         |   12 ++++++++++--
 validation/backend/void-return-type.c |   13 +++++++++++++
 2 files changed, 23 insertions(+), 2 deletions(-)
 create mode 100644 validation/backend/void-return-type.c
sparse-llvm.c +10 -2
index e02e212..213d42d 100644
@@ -150,7 +150,13 @@ static LLVMTypeRef sym_union_type(LLVMModuleRef module, struct symbol *sym)
 
 static LLVMTypeRef sym_ptr_type(LLVMModuleRef module, struct symbol *sym)
 {
-	LLVMTypeRef type = symbol_type(module, sym->ctype.base_type);
+	LLVMTypeRef type;
+
+	/* 'void *' is treated like 'char *' */
+	if (is_void_type(sym->ctype.base_type))
+		type = LLVMInt8Type();
+	else
+		type = symbol_type(module, sym->ctype.base_type);
 
 	return LLVMPointerType(type, 0);
 }
@@ -176,10 +182,12 @@ static LLVMTypeRef sym_basetype_type(struct symbol *sym)
 		}
 	} else {
 		switch (sym->bit_size) {
+		case -1:
+			ret = LLVMVoidType();
+			break;
 		case 1:
 			ret = LLVMInt1Type();
 			break;
-		case -1:	/* 'void *' is treated like 'char *' */
 		case 8:
 			ret = LLVMInt8Type();
 			break;
validation/backend/void-return-type.c new file +13 -0
new file mode 100644
index 0000000..b282fde
@@ -0,0 +1,13 @@
+static void foo(void)
+{
+}
+
+static void *bar(void *p)
+{
+	return p;
+}
+
+/*
+ * check-name: void return type code generation
+ * check-command: ./sparsec -c $file -o tmp.o
+ */
Signature · 6 lines
1.7.7.6

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#1 lore raw
Quoted 75 lines · Pekka Enberg

Jonathan Neuschäfer reports:

   A simple function like this will compile to the following llvm
   bitcode:

         /* C */
         void func(void) {
                 return;
         }

         /* LLVM */
         define i8 @func() {
         L0:
           ret void
         }

   The return type of the function and the type in the return instruction
   don't match.

   I found this inconsistency by running LLVM's bitcode validation on the
   bitcode produced by sparse-llvm.

Move 'void *' special-casing from sym_basetype_type() to sym_ptr_type() to fix the issue.

Reported by: Jonathan Neuschäfer <[email protected]>

Cc Christopher Li <[email protected]>
Cc Jeff Garzik <[email protected]>
Cc Linus Torvalds <[email protected]>
---
  sparse-llvm.c                         |   12 ++++++++++--
  validation/backend/void-return-type.c |   13 +++++++++++++
  2 files changed, 23 insertions(+), 2 deletions(-)

create mode 100644 validation/backend/void-return-type.c

sparse-llvm.c +10 -2
index e02e212..213d42d 100644
@@ -150,7 +150,13 @@ static LLVMTypeRef sym_union_type(LLVMModuleRef module, struct symbol *sym)
 
  static LLVMTypeRef sym_ptr_type(LLVMModuleRef module, struct symbol *sym)
  {
-	LLVMTypeRef type = symbol_type(module, sym->ctype.base_type);
+	LLVMTypeRef type;
+
+	/* 'void *' is treated like 'char *' */
+	if (is_void_type(sym->ctype.base_type))
+		type = LLVMInt8Type();
+	else
+		type = symbol_type(module, sym->ctype.base_type);
 
  	return LLVMPointerType(type, 0);
  }
@@ -176,10 +182,12 @@ static LLVMTypeRef sym_basetype_type(struct symbol *sym)
  		}
  	} else {
  		switch (sym->bit_size) {
+		case -1:
+			ret = LLVMVoidType();
+			break;
  		case 1:
  			ret = LLVMInt1Type();
  			break;
-		case -1:	/* 'void *' is treated like 'char *' */
  		case 8:
  			ret = LLVMInt8Type();
  			break;
validation/backend/void-return-type.c new file +0 -0
new file mode 100644
index 0000000..b282fde

validation/backend/void-return-type.c line 9

Quoted 9 lines
validation/backend/void-return-type.c new file +9 -0
@@ -0,0 +1,13 @@
+static void foo(void)
+{
+}
+
+static void *bar(void *p)
+{
+	return p;
+}
+

ACK the code... would prefer that the first testcase included a "return;" just to be certain we get that right.

Jeff

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html