Mesa (master): gallivm: Cleanup/simplify lp_build_const_string_variable.

Jose Fonseca jrfonseca at kemper.freedesktop.org
Wed May 2 09:25:58 UTC 2012


Module: Mesa
Branch: master
Commit: 0005bd9da2b343accad423708eba36a00035c7ee
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=0005bd9da2b343accad423708eba36a00035c7ee

Author: José Fonseca <jfonseca at vmware.com>
Date:   Thu Feb 23 09:44:41 2012 +0000

gallivm: Cleanup/simplify lp_build_const_string_variable.

- Move to lp_bld_const where it belongs
- Rename to lp_build_const_string
- take the length from the argument (and don't count the zero terminator twice)
- bitcast the constant to generic i8 *

---

 src/gallium/auxiliary/gallivm/lp_bld_assert.c |    6 +++---
 src/gallium/auxiliary/gallivm/lp_bld_const.c  |   18 ++++++++++++++++++
 src/gallium/auxiliary/gallivm/lp_bld_const.h  |    3 +++
 src/gallium/auxiliary/gallivm/lp_bld_printf.c |   22 ++--------------------
 src/gallium/auxiliary/gallivm/lp_bld_printf.h |    9 +++------
 5 files changed, 29 insertions(+), 29 deletions(-)

diff --git a/src/gallium/auxiliary/gallivm/lp_bld_assert.c b/src/gallium/auxiliary/gallivm/lp_bld_assert.c
index 9de5e8e..449d0a7 100644
--- a/src/gallium/auxiliary/gallivm/lp_bld_assert.c
+++ b/src/gallium/auxiliary/gallivm/lp_bld_assert.c
@@ -29,6 +29,7 @@
 #include "util/u_memory.h"
 #include "lp_bld_assert.h"
 #include "lp_bld_init.h"
+#include "lp_bld_const.h"
 #include "lp_bld_printf.h"
 
 
@@ -66,8 +67,7 @@ lp_build_assert(struct gallivm_state *gallivm,
    LLVMTypeRef arg_types[2];
    LLVMValueRef msg_string, assert_func, params[2], r;
 
-   msg_string = lp_build_const_string_variable(module, context,
-                                               msg, strlen(msg) + 1);
+   msg_string = lp_build_const_string(gallivm, msg);
 
    arg_types[0] = LLVMInt32TypeInContext(context);
    arg_types[1] = LLVMPointerType(LLVMInt8TypeInContext(context), 0);
@@ -90,7 +90,7 @@ lp_build_assert(struct gallivm_state *gallivm,
 
    /* build function call param list */
    params[0] = LLVMBuildZExt(builder, condition, arg_types[0], "");
-   params[1] = LLVMBuildBitCast(builder, msg_string, arg_types[1], "");
+   params[1] = msg_string;
 
    /* check arg types */
    assert(LLVMTypeOf(params[0]) == arg_types[0]);
diff --git a/src/gallium/auxiliary/gallivm/lp_bld_const.c b/src/gallium/auxiliary/gallivm/lp_bld_const.c
index 77ec1a7..9e9dc44 100644
--- a/src/gallium/auxiliary/gallivm/lp_bld_const.c
+++ b/src/gallium/auxiliary/gallivm/lp_bld_const.c
@@ -428,3 +428,21 @@ lp_build_const_mask_aos_swizzled(struct gallivm_state *gallivm,
 
    return lp_build_const_mask_aos(gallivm, type, mask);
 }
+
+
+/**
+ * Build a zero-terminated constant string.
+ */
+LLVMValueRef
+lp_build_const_string(struct gallivm_state *gallivm,
+                      const char *str)
+{
+   unsigned len = strlen(str) + 1;
+   LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
+   LLVMValueRef string = LLVMAddGlobal(gallivm->module, LLVMArrayType(i8, len), "");
+   LLVMSetGlobalConstant(string, TRUE);
+   LLVMSetLinkage(string, LLVMInternalLinkage);
+   LLVMSetInitializer(string, LLVMConstStringInContext(gallivm->context, str, len, TRUE));
+   string = LLVMConstBitCast(string, LLVMPointerType(i8, 0));
+   return string;
+}
diff --git a/src/gallium/auxiliary/gallivm/lp_bld_const.h b/src/gallium/auxiliary/gallivm/lp_bld_const.h
index fd39851..34c3c69 100644
--- a/src/gallium/auxiliary/gallivm/lp_bld_const.h
+++ b/src/gallium/auxiliary/gallivm/lp_bld_const.h
@@ -149,5 +149,8 @@ lp_build_const_int_pointer(struct gallivm_state *gallivm, const void *ptr)
 }
 
 
+LLVMValueRef
+lp_build_const_string(struct gallivm_state *gallivm,
+                      const char *str);
 
 #endif /* !LP_BLD_CONST_H */
diff --git a/src/gallium/auxiliary/gallivm/lp_bld_printf.c b/src/gallium/auxiliary/gallivm/lp_bld_printf.c
index 5aa802d..806b8e0 100644
--- a/src/gallium/auxiliary/gallivm/lp_bld_printf.c
+++ b/src/gallium/auxiliary/gallivm/lp_bld_printf.c
@@ -66,19 +66,6 @@ lp_get_printf_arg_count(const char *fmt)
    return count;
 }
 
-LLVMValueRef 
-lp_build_const_string_variable(LLVMModuleRef module,
-                               LLVMContextRef context,
-                               const char *str, int len)
-{
-   LLVMValueRef string = LLVMAddGlobal(module, LLVMArrayType(LLVMInt8TypeInContext(context), len + 1), "");
-   LLVMSetGlobalConstant(string, TRUE);
-   LLVMSetLinkage(string, LLVMInternalLinkage);
-   LLVMSetInitializer(string, LLVMConstStringInContext(context, str, len + 1, TRUE));
-   return string;
-}
- 
-
 /**
  * lp_build_printf.
  *
@@ -96,22 +83,17 @@ lp_build_printf(struct gallivm_state *gallivm, const char *fmt, ...)
    LLVMContextRef context = gallivm->context;
    LLVMModuleRef module = gallivm->module;
    LLVMValueRef params[50];
-   LLVMValueRef fmtarg = lp_build_const_string_variable(module, context,
-                                                        fmt, strlen(fmt) + 1);
-   LLVMValueRef int0 = lp_build_const_int32(gallivm, 0);
-   LLVMValueRef index[2];
+   LLVMValueRef fmtarg = lp_build_const_string(gallivm, fmt);
    LLVMValueRef func_printf = LLVMGetNamedFunction(module, "printf");
 
    assert(Elements(params) >= argcount + 1);
 
-   index[0] = index[1] = int0;
-
    if (!func_printf) {
       LLVMTypeRef printf_type = LLVMFunctionType(LLVMIntTypeInContext(context, 32), NULL, 0, 1);
       func_printf = LLVMAddFunction(module, "printf", printf_type);
    }
 
-   params[0] = LLVMBuildGEP(builder, fmtarg, index, 2, "");
+   params[0] = fmtarg;
 
    va_start(arglist, fmt);
    for (i = 1; i <= argcount; i++) {
diff --git a/src/gallium/auxiliary/gallivm/lp_bld_printf.h b/src/gallium/auxiliary/gallivm/lp_bld_printf.h
index ec087fd..7a2b26d 100644
--- a/src/gallium/auxiliary/gallivm/lp_bld_printf.h
+++ b/src/gallium/auxiliary/gallivm/lp_bld_printf.h
@@ -34,12 +34,9 @@
 #include "lp_bld_init.h"
 
 
-LLVMValueRef lp_build_const_string_variable(LLVMModuleRef module,
-                                            LLVMContextRef context,
-                                            const char *str, int len);
-
-LLVMValueRef lp_build_printf(struct gallivm_state *gallivm,
-                             const char *fmt, ...);
+LLVMValueRef
+lp_build_printf(struct gallivm_state *gallivm,
+                const char *fmt, ...);
 
 LLVMValueRef
 lp_build_print_vec4(struct gallivm_state *gallivm,




More information about the mesa-commit mailing list