Skip to contentSkip to conversation

sparse, llvm: Simplify output_data() type logic

PATCH In review Pekka Enberg 2 participants 3 messages Jun 8, 2012 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 sparse, llvm: Simplify output_data() type logic

Use LLVMTypeOf() in output_data() to simplify type handling logic.

Cc Benjamin Herrenschmidt <[email protected]>
Cc Christopher Li <[email protected]>
Cc Jeff Garzik <[email protected]>
---
 sparse-llvm.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
sparse-llvm.c +1 -1
index 9226a21..a39bc02 100644
@@ -1192,7 +1192,7 @@ static LLVMValueRef output_data(LLVMModuleRef module, struct symbol *sym)
 
 	name = show_ident(sym->ident);
 
-	data = LLVMAddGlobal(module, symbol_type(module, sym->ctype.base_type), name);
+	data = LLVMAddGlobal(module, LLVMTypeOf(initial_value), name);
 
 	LLVMSetLinkage(data, data_linkage(sym));
 
Signature · 1 line
1.7.7.6
#1 lore raw

PATCH 2/2 sparse, llvm: Fix string initializer code generation

This fixes code generation for string initializer such as:

  static char *foo = "Foo !\n";

It's the same fix Ben proposed earlier with the small difference that we now use LLVMTypeOf() instead of symbol_type() to resolve the type.

The generated LLVM IR looks as follows:

[penberg@tux sparse]$ ./sparse-llvm foo.c |llvm-dis ; ModuleID = '<stdin>'

@"<noident>" = private global [7 x i8] c"Foo !\0A\00" @foo = private global [7 x i8]* @"<noident>"

Cc Benjamin Herrenschmidt <[email protected]>
Cc Christopher Li <[email protected]>
Cc Jeff Garzik <[email protected]>
---
 sparse-llvm.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
sparse-llvm.c +6 -0
index a39bc02..89c6a2e 100644
@@ -1181,6 +1181,12 @@ static LLVMValueRef output_data(LLVMModuleRef module, struct symbol *sym)
 				initial_value = output_data(module, sym);
 			break;
 		}
+		case EXPR_STRING: {
+			const char *s = initializer->string->data;
+
+			initial_value = LLVMConstString(strdup(s), strlen(s) + 1, true);
+			break;
+		}
 		default:
 			assert(0);
 		}
Signature · 1 line
1.7.7.6
#2 lore raw

Re: [PATCH 2/2] sparse, llvm: Fix string initializer code generation

Quoted 29 lines · Pekka Enberg

This fixes code generation for string initializer such as:

   static char *foo = "Foo !\n";

It's the same fix Ben proposed earlier with the small difference that we now use LLVMTypeOf() instead of symbol_type() to resolve the type.

The generated LLVM IR looks as follows:

   [penberg@tux sparse]$ ./sparse-llvm foo.c |llvm-dis
   ; ModuleID = '<stdin>'

   @"<noident>" = private global [7 x i8] c"Foo !\0A\00"
   @foo = private global [7 x i8]* @"<noident>"
Cc Benjamin Herrenschmidt<[email protected]>
Cc Christopher Li<[email protected]>
Cc Jeff Garzik<[email protected]>
---
  sparse-llvm.c |    6 ++++++
  1 files changed, 6 insertions(+), 0 deletions(-)
sparse-llvm.c +0 -0
index a39bc02..89c6a2e 100644

sparse-llvm.c static LLVMValueRef output_data(LLVMModuleRef module, struct symbol *sym) · line 1190

Quoted 10 lines
sparse-llvm.c +6 -0
@@ -1181,6 +1181,12 @@ static LLVMValueRef output_data(LLVMModuleRef module, struct symbol *sym)
  				initial_value = output_data(module, sym);
  			break;
  		}
+		case EXPR_STRING: {
+			const char *s = initializer->string->data;
+
+			initial_value = LLVMConstString(strdup(s), strlen(s) + 1, true);
+			break;
+		}
  		default:

ACK 1-2